home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-04d.zip / 04d / Hity z okladki / Plus Firma / AccessRT / OSP1.CAB / INSTCAT.SQL < prev    next >
Text File  |  1999-10-12  |  616KB  |  20,632 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1999, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. j is the major version number ('7' now), nn is the minor version number
  12. ('00' now), and bbb is the build number.
  13. insert into spt_server_info
  14.     values (500, 'SYS_SPROC_VERSION', '7.00.bbb')
  15. */
  16.  
  17. /****************************************************************************/
  18. /* This portion sets up the ability to perform all the functions in this    */
  19. /* script                                                                    */
  20. /****************************************************************************/
  21. go
  22. use master
  23. go
  24. dump tran master with no_log
  25. go
  26.  
  27. set quoted_identifier on
  28. go
  29.  
  30. if (exists (select * from sysobjects
  31.         where name = 'sp_configure' and type = 'P '))
  32. begin
  33.     execute sp_configure 'update',1
  34. end
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  39.  
  40. go
  41.  
  42. /*
  43. ** If old versions of tables exist, drop them.
  44. */
  45. if (exists (select * from sysobjects
  46.         where name = 'MSdatatype_info' and type = 'U '))
  47.     drop table MSdatatype_info
  48. go
  49. if (exists (select * from sysobjects
  50.         where name = 'MSdatatype_info_ext' and type = 'U '))
  51.     drop table MSdatatype_info_ext
  52. go
  53. if (exists (select * from sysobjects
  54.         where name = 'MStable_types' and type = 'U '))
  55.     drop table MStable_types
  56. go
  57. if (exists (select * from sysobjects
  58.         where name = 'MSserver_info' and type = 'U '))
  59.     drop table MSserver_info
  60. go
  61. if (exists (select * from sysobjects
  62.         where name = 'spt_table_types' and type = 'U '))
  63.     drop table spt_table_types
  64. go
  65. /*
  66. ** If tables or procs already exist, drop them.
  67. */
  68.  
  69. if (exists (select * from sysobjects
  70.         where name = 'spt_datatype_info' and type = 'U '))
  71.     drop table spt_datatype_info
  72. go
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info_ext' and type = 'U '))
  75.     drop table spt_datatype_info_ext
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  79.     drop proc sp_add_server_sortinfo
  80. go
  81. if (exists (select * from sysobjects
  82.         where name = 'spt_server_info' and type = 'U '))
  83.     drop table spt_server_info
  84. go
  85. if (exists (select * from sysobjects
  86.         where name = 'sp_tables' and type = 'P '))
  87.     drop proc sp_tables
  88. go
  89. if (exists (select * from sysobjects
  90.         where name = 'sp_statistics' and type = 'P '))
  91.     drop proc sp_statistics
  92. go
  93. if (exists (select * from sysobjects
  94.         where name = 'sp_columns' and type = 'P '))
  95.     drop proc sp_columns
  96. go
  97. if (exists (select * from sysobjects
  98.         where name = 'sp_fkeys' and type = 'P '))
  99.     drop proc sp_fkeys
  100. go
  101. if (exists (select * from sysobjects
  102.         where name = 'sp_pkeys' and type = 'P '))
  103.     drop proc sp_pkeys
  104. dump tran master with no_log
  105. go
  106.  
  107. go
  108. if (exists (select * from sysobjects
  109.         where name = 'sp_stored_procedures' and type = 'P '))
  110.     drop proc sp_stored_procedures
  111. go
  112. if (exists (select * from sysobjects
  113.         where name = 'sp_sproc_columns' and type = 'P '))
  114.     drop proc sp_sproc_columns
  115. go
  116. if (exists (select * from sysobjects
  117.         where name = 'sp_table_privileges' and type = 'P '))
  118.     drop proc sp_table_privileges
  119. go
  120. if (exists (select * from sysobjects
  121.         where name = 'sp_column_privileges' and type = 'P '))
  122.     drop proc sp_column_privileges
  123. go
  124. dump tran master with no_log
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_server_info' and type = 'P '))
  128.     drop proc sp_server_info
  129. go
  130. if (exists (select * from sysobjects
  131.         where name = 'sp_datatype_info' and type = 'P '))
  132.     drop proc sp_datatype_info
  133. go
  134. if (exists (select * from sysobjects
  135.         where name = 'sp_special_columns' and type = 'P '))
  136.     drop proc sp_special_columns
  137. go
  138. if (exists (select * from sysobjects
  139.         where name = 'sp_databases' and type = 'P '))
  140.     drop proc sp_databases
  141. go
  142. dump tran master with no_log
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_ddopen' and type = 'P '))
  146.     drop proc sp_ddopen
  147. go
  148. if (exists (select * from sysobjects
  149.         where name = 'sp_tableswc' and type = 'P '))
  150.     drop proc sp_tableswc
  151. go
  152.  
  153. dump tran master with no_log
  154. go
  155.  
  156.  
  157. if (exists (select * from sysobjects
  158.         where name = 'spt_provider_types' and type = 'U '))
  159.     begin
  160.     drop table spt_provider_types
  161.     dump tran master with no_log
  162.     end
  163. go
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_catalogs_rowset' and type = 'P '))
  166.     begin
  167.     drop procedure sp_catalogs_rowset
  168.     dump tran master with no_log
  169.     end
  170. go
  171. if (exists (select * from sysobjects
  172.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  173.     begin
  174.     drop procedure sp_column_privileges_rowset
  175.     dump tran master with no_log
  176.     end
  177. go
  178. if (exists (select * from sysobjects
  179.         where name = 'sp_columns_rowset' and type = 'P '))
  180.     begin
  181.     drop procedure sp_columns_rowset
  182.     dump tran master with no_log
  183.     end
  184. go
  185. if (exists (select * from sysobjects
  186.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  187.     begin
  188.     drop procedure sp_foreign_keys_rowset
  189.     dump tran master with no_log
  190.     end
  191. go
  192. if (exists (select * from sysobjects
  193.         where name = 'sp_indexes_rowset' and type = 'P '))
  194.     begin
  195.     drop procedure sp_indexes_rowset
  196.     dump tran master with no_log
  197.     end
  198. go
  199. if (exists (select * from sysobjects
  200.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  201.     begin
  202.     drop procedure sp_primary_keys_rowset
  203.     dump tran master with no_log
  204.     end
  205. go
  206. if (exists (select * from sysobjects
  207.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  208.     begin
  209.     drop procedure sp_procedure_params_rowset
  210.     dump tran master with no_log
  211.     end
  212. go
  213. if (exists (select * from sysobjects
  214.         where name = 'sp_procedures_rowset' and type = 'P '))
  215.     begin
  216.     drop procedure sp_procedures_rowset
  217.     dump tran master with no_log
  218.     end
  219. go
  220. if (exists (select * from sysobjects
  221.         where name = 'sp_provider_types_rowset' and type = 'P '))
  222.     begin
  223.     drop procedure sp_provider_types_rowset
  224.     dump tran master with no_log
  225.     end
  226. go
  227. if (exists (select * from sysobjects
  228.         where name = 'sp_schemata_rowset' and type = 'P '))
  229.     begin
  230.     drop procedure sp_schemata_rowset
  231.     dump tran master with no_log
  232.     end
  233. go
  234. if (exists (select * from sysobjects
  235.         where name = 'sp_statistics_rowset' and type = 'P '))
  236.     begin
  237.     drop procedure sp_statistics_rowset
  238.     dump tran master with no_log
  239.     end
  240. go
  241. if (exists (select * from sysobjects
  242.         where name = 'sp_tables_rowset' and type = 'P '))
  243.     begin
  244.     drop procedure sp_tables_rowset
  245.     dump tran master with no_log
  246.     end
  247. go
  248. if (exists (select * from sysobjects
  249.         where name = 'sp_tables_info_rowset' and type = 'P '))
  250.     begin
  251.     drop procedure sp_tables_info_rowset
  252.     dump tran master with no_log
  253.     end
  254. go
  255. if (exists (select * from sysobjects
  256.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  257.     begin
  258.     drop proc sp_table_constraints_rowset
  259.     dump tran master with no_log
  260.     end
  261. go
  262. if (exists (select * from sysobjects
  263.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  264.     begin
  265.     drop proc sp_table_privileges_rowset
  266.     dump tran master with no_log
  267.     end
  268. go
  269. if (exists (select * from sysobjects
  270.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  271.     begin
  272.     drop proc sp_linkedservers_rowset
  273.     dump tran master with no_log
  274.     end
  275. go
  276.  
  277.  
  278. print 'creating table spt_datatype_info_ext'
  279. go
  280.  
  281. if (charindex('6.00', @@version) = 0 and
  282.     charindex('6.50', @@version) = 0 and
  283.     charindex('7.00', @@version) = 0)
  284. begin    /*    Pre 6.0 server */
  285.     print ''
  286.     print ''
  287.     print 'Warning:'
  288.     print 'you are installing the stored procedures '
  289.     print 'on a pre 6.0 SQL Server.'
  290.     print 'Ignore the following error.'
  291.     create table spt_datatype_info_ext (
  292.                 user_type        smallint    not null,
  293.                 CREATE_PARAMS    varchar(32) null,
  294.                 AUTO_INCREMENT    smallint null,
  295.                 typename        varchar(32))
  296. end
  297. go
  298. if (charindex('6.00', @@version) > 0 or
  299.     charindex('6.50', @@version) > 0 or
  300.     charindex('7.00', @@version) > 0)
  301. begin    /* 6.0 or later server */
  302.     create table spt_datatype_info_ext (
  303.                 user_type        smallint    not null,
  304.                 CREATE_PARAMS    varchar(32) null,
  305.                 AUTO_INCREMENT    smallint null,
  306.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  307. end
  308. go
  309.  
  310. grant select on spt_datatype_info_ext to public
  311. go
  312.  
  313.  
  314. insert into spt_datatype_info_ext
  315.     /* CHAR      user_type, create_params, auto_increment */
  316.     values             (1,    'length' ,0, 'char')
  317.  
  318. insert into spt_datatype_info_ext
  319.     /* VARCHAR     user_type, create_params, auto_increment */
  320.     values             (2,    'max length' ,0, 'varchar')
  321.  
  322. insert into spt_datatype_info_ext
  323.     /* BINARY     user_type, create_params, auto_increment */
  324.     values             (3,    'length' ,0, 'binary')
  325.  
  326. insert into spt_datatype_info_ext
  327.     /* VARBINARY user_type, create_params, auto_increment */
  328.     values             (4,    'max length' ,0, 'varbinary')
  329.  
  330. if    (charindex('6.00', @@version) > 0 or
  331.      charindex('6.50', @@version) > 0 or
  332.      charindex('7.00', @@version) > 0)
  333. begin    /*    Add 6.0 data types */
  334.     insert into spt_datatype_info_ext
  335.         /* DECIMAL user_type, create_params, auto_increment */
  336.         values             (26,    'precision,scale' ,0, 'decimal')
  337.  
  338.     insert into spt_datatype_info_ext
  339.         /* NUMERIC user_type, create_params, auto_increment */
  340.         values             (25,    'precision,scale' ,0, 'numeric')
  341.  
  342.     insert into spt_datatype_info_ext
  343.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  344.         values             (26,    'precision' ,1, 'decimal')
  345.  
  346.     insert into spt_datatype_info_ext
  347.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  348.         values             (25,    'precision' ,1, 'numeric')
  349.  
  350. end
  351. else    /*    Pre 6.0 server, add SYSNAME create param */
  352.     begin
  353.         insert into spt_datatype_info_ext
  354.             /* SYSNAME     user_type, create_param, auto_increments */
  355.             values             (18,    'max length' ,0, 'sysname')
  356.  
  357.     end
  358. go
  359.  
  360. if (charindex('7.00', @@version) = 0)
  361. begin
  362.     print ''
  363.     print ''
  364.     print 'Warning:'
  365.     print 'you are installing the stored procedures '
  366.     print 'on a pre 7.0 SQL Server.'
  367.     print 'Ignore the following errors.'
  368. end
  369. go
  370. if (charindex('7.00', @@version) > 0)
  371. begin    /*    Update usertypes for 7.00 server */
  372.     begin tran
  373.     insert into spt_datatype_info_ext
  374.         /* NCHAR     user_type, create_params, auto_increment */
  375.         values             (0,    'length' ,0, 'nchar')
  376.  
  377.     insert into spt_datatype_info_ext
  378.         /* NVARCHAR     user_type, create_params, auto_increment */
  379.         values             (0,    'max length' ,0, 'nvarchar')
  380.  
  381.     /* SET user_type TO SPHINX VALUES */
  382.     update spt_datatype_info_ext set user_type = xusertype
  383.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  384.  
  385.     commit tran
  386. end
  387. go
  388.  
  389. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  390. go
  391.  
  392. print 'creating table spt_datatype_info'
  393. go
  394. if (charindex('6.00', @@version) = 0 and
  395.     charindex('6.50', @@version) = 0 and
  396.     charindex('7.00', @@version) = 0)
  397. begin    /*    Pre 6.0 server */
  398.     print ''
  399.     print ''
  400.     print 'Warning:'
  401.     print 'you are installing the stored procedures '
  402.     print 'on a pre 6.0 SQL Server.'
  403.     print 'Ignore the following error.'
  404.     create table spt_datatype_info (
  405.         ss_dtype            tinyint     not null,
  406.         fixlen                int         null,        /* datatype len for variable, else null */
  407.         ODBCVer             tinyint     null,        /* version if needed, else null */
  408.         TYPE_NAME            varchar(32)    not null,
  409.         DATA_TYPE            smallint    not null,
  410.         data_precision        int         null,
  411.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  412.         RADIX                smallint    null,
  413.         length                int         null,
  414.         LITERAL_PREFIX        varchar(32) null,
  415.         LITERAL_SUFFIX        varchar(32) null,
  416.         CREATE_PARAMS        varchar(32) null,
  417.         NULLABLE            smallint    not null,
  418.         CASE_SENSITIVE        smallint    not null,
  419.         SEARCHABLE            smallint    not null,
  420.         UNSIGNED_ATTRIBUTE    smallint    null,
  421.         MONEY                smallint    not null,
  422.         AUTO_INCREMENT        smallint    null,
  423.         LOCAL_TYPE_NAME     varchar(32)    null,
  424.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  425.         SQL_DATA_TYPE        smallint    not null,
  426.         SQL_DATETIME_SUB    smallint    null)
  427. end
  428. go
  429. if (charindex('6.00', @@version) > 0 or
  430.     charindex('6.50', @@version) > 0 or
  431.     charindex('7.00', @@version) > 0)
  432. begin    /* 6.0 or later server */
  433.     create table spt_datatype_info (
  434.         ss_dtype            tinyint     not null,
  435.         fixlen                int         null,        /* datatype len for variable, else null */
  436.         ODBCVer             tinyint     null,        /* version if needed, else null */
  437.         TYPE_NAME            sysname     not null,
  438.         DATA_TYPE            smallint    not null,
  439.         data_precision        int         null,
  440.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  441.         RADIX                smallint    null,
  442.         length                int         null,
  443.         LITERAL_PREFIX        varchar(32) null,
  444.         LITERAL_SUFFIX        varchar(32) null,
  445.         CREATE_PARAMS        varchar(32) null,
  446.         NULLABLE            smallint    not null,
  447.         CASE_SENSITIVE        smallint    not null,
  448.         SEARCHABLE            smallint    not null,
  449.         UNSIGNED_ATTRIBUTE    smallint    null,
  450.         MONEY                smallint    not null,
  451.         AUTO_INCREMENT        smallint    null,
  452.         LOCAL_TYPE_NAME     sysname     null,
  453.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  454.         SQL_DATA_TYPE        smallint    not null,
  455.         SQL_DATETIME_SUB    smallint    null)
  456. end
  457. go
  458.  
  459. grant select on spt_datatype_info to public
  460. go
  461.  
  462. /* Get case sensitivity */
  463. if 'A' = 'A' /* create dummy begin block */
  464. begin
  465.     declare @case smallint
  466.  
  467.     begin tran
  468.     select @case = 0
  469.     select @case = 1 where 'a' <> 'A'
  470.  
  471.     /* Local Binary */
  472.     insert into spt_datatype_info values
  473.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  474.  
  475.     /* Local Bit */
  476.     insert into spt_datatype_info values
  477.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  478.  
  479.     /* Local Char */
  480.     insert into spt_datatype_info values
  481.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  482.  
  483.     /* Local Datetime */
  484.     insert into spt_datatype_info values
  485.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  486.     insert into spt_datatype_info values
  487.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  488.  
  489.     /* Local Smalldatetime */
  490.     insert into spt_datatype_info values
  491.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  492.     insert into spt_datatype_info values
  493.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  494.  
  495.     /* Local Float */
  496.     insert into spt_datatype_info values
  497.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  498.     insert into spt_datatype_info values
  499.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  500.  
  501.     /* Local Real */
  502.     insert into spt_datatype_info values
  503.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  504.     insert into spt_datatype_info values
  505.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  506.  
  507.     /* Local Smallmoney */
  508.     insert into spt_datatype_info values
  509.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  510.  
  511.     /* Local Money */
  512.     insert into spt_datatype_info values
  513.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  514.  
  515.     /* Local Int */
  516.     insert into spt_datatype_info values
  517.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  518.  
  519.     commit tran
  520. end
  521. go
  522.  
  523. if 'A' = 'A' /* create dummy begin block */
  524. begin
  525.     declare @case smallint
  526.  
  527.     begin tran
  528.     select @case = 0
  529.     select @case = 1 where 'a' <> 'A'
  530.  
  531.  
  532.     /* Local Smallint */
  533.     insert into spt_datatype_info values
  534.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  535.     insert into spt_datatype_info values
  536.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  537.  
  538.     /* Local Tinyint */
  539.     insert into spt_datatype_info values
  540.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  541.  
  542.     /* Local Text */
  543.     insert into spt_datatype_info values
  544.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  545.  
  546.     /* Local Varbinary */
  547.     insert into spt_datatype_info values
  548.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  549.  
  550.     /* Local Varchar */
  551.     insert into spt_datatype_info values
  552.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  553.  
  554.     /* Local Image */
  555.     insert into spt_datatype_info values
  556.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  557.  
  558.     if (charindex('6.00', @@version) > 0 or
  559.         charindex('6.50', @@version) > 0 or
  560.         charindex('7.00', @@version) > 0)
  561.     begin    /*    Add 6.0 data types */
  562.         /* Local Decimal */
  563.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  564.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  565.  
  566.         /* Local Numeric */
  567.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  568.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  569.  
  570.         /* Identity attribute data types */
  571.  
  572.         /* Identity Int */
  573.         insert into spt_datatype_info values
  574.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  575.  
  576.         /* Identity Smallint */
  577.         insert into spt_datatype_info values
  578.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  579.  
  580.         /* Identity Tinyint */
  581.         insert into spt_datatype_info values
  582.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  583.  
  584.         /* Identity Numeric */
  585.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  586.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  587.  
  588.         /* Identity Numeric */
  589.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  590.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  591.  
  592.     end
  593.  
  594.     if (charindex('7.00', @@version) = 0)
  595.     begin    /*    Add nullable type for non-Sphinx server */
  596.         /* Local Datetimn */
  597.         insert into spt_datatype_info values
  598.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  599.         insert into spt_datatype_info values
  600.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  601.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  602.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  603.         insert into spt_datatype_info values
  604.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  605.  
  606.         /* Local Floatn */
  607.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  608.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  609.         insert into spt_datatype_info values
  610.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  611.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  612.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  613.         insert into spt_datatype_info values
  614.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  615.  
  616.         /* Local Moneyn */
  617.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  618.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  619.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  620.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  621.  
  622.         /* Local Intn */
  623.         insert into spt_datatype_info values /* sql server type is 'intn' */
  624.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  625.         insert into spt_datatype_info values /* sql server type is 'intn' */
  626.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  627.         insert into spt_datatype_info values
  628.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  629.  
  630.         if (charindex('6.00', @@version) > 0 or
  631.             charindex('6.50', @@version) > 0 or
  632.             charindex('7.00', @@version) > 0)
  633.         begin    /*    Add 6.0 data types */
  634.             /* Local Decimaln */
  635.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  636.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  637.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  638.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  639.  
  640.             /* Local Numericn */
  641.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  642.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  643.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  644.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  645.         end
  646.     end
  647.  
  648.     commit tran
  649. end
  650. go
  651.  
  652. if (charindex('7.00', @@version) = 0)
  653. begin
  654.     print ''
  655.     print ''
  656.     print 'Warning:'
  657.     print 'you are installing the stored procedures '
  658.     print 'on a pre 7.0 SQL Server.'
  659.     print 'Ignore the following errors.'
  660. end
  661. go
  662. if (charindex('7.00', @@version) > 0)
  663.     begin
  664.         declare @ncase smallint
  665.  
  666.         select @ncase = 0
  667.         select @ncase = 1 where N'a' <> N'A'
  668.  
  669.         /* Local Timestamp */
  670.         insert into spt_datatype_info values
  671.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  672.  
  673.         /* Local GUID */
  674.         insert into spt_datatype_info values
  675.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  676.  
  677.         /* Local NChar */
  678.         insert into spt_datatype_info values
  679.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  680.  
  681.         /* Local NVarchar */
  682.         insert into spt_datatype_info values
  683.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  684.  
  685.         /* Local NText */
  686.         insert into spt_datatype_info values
  687.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  688.  
  689.         update spt_datatype_info set NULLABLE = 1
  690.             where TYPE_NAME = 'bit'
  691.  
  692.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  693.             where TYPE_NAME like name+'%')
  694.     end
  695. go
  696.  
  697. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  698. go
  699.  
  700. dump tran master with no_log
  701. go
  702.  
  703. print 'creating table spt_server_info'
  704. go
  705. create table spt_server_info (
  706.               attribute_id        int NOT NULL,
  707.               attribute_name    varchar(60) NOT NULL,
  708.               attribute_value    varchar(255) NOT NULL)
  709. go
  710.  
  711. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  712. go
  713.  
  714. if (charindex('7.00', @@version) = 0)
  715.     begin
  716.         drop procedure sp_add_server_sortinfo    /* not used by older servers */
  717.         dump tran master with no_log
  718.     end
  719. go
  720.  
  721.  
  722. insert into spt_server_info
  723.     values (1,'DBMS_NAME','Microsoft SQL Server')
  724. insert into spt_server_info
  725.     values (2,'DBMS_VER',@@version)
  726. insert into spt_server_info
  727.     values (10,'OWNER_TERM','owner')
  728. insert into spt_server_info
  729.     values (11,'TABLE_TERM','table')
  730. insert into spt_server_info
  731.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  732. insert into spt_server_info
  733.     values (13,'TABLE_LENGTH','30')
  734. insert into spt_server_info
  735.     values (14,'MAX_QUAL_LENGTH','30')
  736. insert into spt_server_info
  737.     values (15,'COLUMN_LENGTH','30')
  738. if 'A' = 'a' /* If not case sensitive server */
  739. begin
  740.     insert into spt_server_info
  741.         values (16,'IDENTIFIER_CASE','MIXED')
  742. end
  743. else
  744. begin
  745.     insert into spt_server_info
  746.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  747. end
  748. insert into spt_server_info
  749.     values (17,'TX_ISOLATION','2')
  750. if (charindex('6.00', @@version) > 0 or
  751.     charindex('6.50', @@version) > 0 or
  752.     charindex('7.00', @@version) > 0)
  753. begin    /*    Add 6.0 collation sequence */
  754.     insert into spt_server_info
  755.         select 18,'COLLATION_SEQ',
  756.             'charset='+t2.name+' sort_order='+t1.name
  757.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  758.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  759.         from syscharsets t1, syscharsets t2, sysconfigures t3
  760.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  761. end
  762. else
  763. begin    /*    Add 4.2x collation sequence */
  764.     insert into spt_server_info
  765.         select 18,'COLLATION_SEQ',
  766.             'charset='+t2.name+' sort_order='+t1.name
  767.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  768.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  769.         from syscharsets t1, syscharsets t2, sysconfigures t3
  770.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  771. end
  772. insert into spt_server_info
  773.     values (19,'SAVEPOINT_SUPPORT','Y')
  774. insert into spt_server_info
  775.     values (20,'MULTI_RESULT_SETS','Y')
  776. insert into spt_server_info
  777.     values (22,'ACCESSIBLE_TABLES','Y')
  778. go
  779.  
  780. insert into spt_server_info
  781.     values (100,'USERID_LENGTH','30')
  782. insert into spt_server_info
  783.     values (101,'QUALIFIER_TERM','database')
  784. insert into spt_server_info
  785.     values (102,'NAMED_TRANSACTIONS','Y')
  786. insert into spt_server_info
  787.     values (103,'SPROC_AS_LANGUAGE','Y')
  788. insert into spt_server_info
  789.     values (104,'ACCESSIBLE_SPROC','Y')
  790. insert into spt_server_info
  791.     values (105,'MAX_INDEX_COLS','16')
  792. insert into spt_server_info
  793.     values (106,'RENAME_TABLE','Y')
  794. insert into spt_server_info
  795.     values (107,'RENAME_COLUMN','Y')
  796. insert into spt_server_info
  797.     values (108,'DROP_COLUMN','N')
  798. insert into spt_server_info
  799.     values (109,'INCREASE_COLUMN_LENGTH','N')
  800. if (charindex('6.50', @@version) = 0 and
  801.     charindex('7.00', @@version) = 0)
  802. begin
  803.     insert into spt_server_info
  804.         values (110,'DDL_IN_TRANSACTION','N')
  805. end
  806. else
  807. begin
  808.     insert into spt_server_info
  809.         values (110,'DDL_IN_TRANSACTION','Y')
  810. end
  811. insert into spt_server_info
  812.     values (111,'DESCENDING_INDEXES','N')
  813. insert into spt_server_info
  814.     values (112,'SP_RENAME','Y')
  815. insert into spt_server_info
  816.     values (113,'REMOTE_SPROC','Y')
  817. insert into spt_server_info
  818.     values (500,'SYS_SPROC_VERSION','7.01.816')
  819. go
  820.  
  821. if (charindex('7.00', @@version) > 0)
  822. begin    /*    Update values for 7.00 server */
  823.     update spt_server_info set attribute_value = '128'
  824.         where attribute_id in (12,13,14,15,100)
  825. end
  826. go
  827.  
  828. grant select on spt_server_info to public
  829. go
  830.  
  831. print 'creating sp_column_privileges'
  832. go
  833.  
  834. /*    Procedure for pre 6.50 server */
  835. CREATE PROCEDURE sp_column_privileges (
  836.             @table_name         varchar(32),
  837.             @table_owner        varchar(32) = null,
  838.             @table_qualifier    varchar(32) = null,
  839.             @column_name        varchar(96) = null)    /* 3*32 */
  840. as
  841.  
  842.     set nocount on
  843.     declare @table_id     int
  844.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  845.     declare @low smallint                     /* range of userids to check */
  846.     declare @high smallint
  847.     declare @owner_uid smallint
  848.  
  849.     select @low = 0, @high = 32767
  850.  
  851.     if @column_name is null /*    If column name not supplied, match all */
  852.         select @column_name = '%'
  853.  
  854.     if @table_qualifier is not null
  855.     begin
  856.         if db_name() <> @table_qualifier
  857.         begin    /* If qualifier doesn't match current database */
  858.             raiserror 20001 '~~Rush_5~~'
  859.             return
  860.         end
  861.     end
  862.     if @table_owner is null
  863.     begin    /* If unqualified table name */
  864.         SELECT @full_table_name = @table_name
  865.     end
  866.     else
  867.     begin    /* Qualified table name */
  868.         SELECT @full_table_name = @table_owner + '.' + @table_name
  869.     end
  870.     /*    Get Object ID */
  871.     select @table_id = object_id(@full_table_name)
  872.  
  873.     if (@@trancount <> 0)
  874.     begin    /* If inside a transaction */
  875.         raiserror 20003 '~~Rush_6~~'
  876.         return
  877.     end
  878.  
  879.     /*
  880.     ** We need to create a table which will contain a row for every row to
  881.     ** be returned to the client.
  882.     */
  883.  
  884.     create table #column_priv1(
  885.         COLUMN_NAME             varchar(32) NOT NULL,
  886.         grantor                 smallint NOT NULL,
  887.         grantee                 smallint NOT NULL,
  888.         select_privilege        bit,
  889.         select_grantable        bit,
  890.         insert_privilege        bit,
  891.         insert_grantable        bit,
  892.         update_privilege        bit,
  893.         update_grantable        bit,
  894.         references_privilege    bit,
  895.         references_grantable    bit,
  896.         uid                     smallint NOT NULL,
  897.         gid                     smallint NOT NULL)
  898.  
  899. /*
  900. ** insert a row for the table owner (who has all permissions)
  901. */
  902.     select @owner_uid = (
  903.         select uid
  904.         from sysobjects
  905.         where id = @table_id)
  906.  
  907.     if (charindex('6.00', @@version) > 0)
  908.     begin
  909.         insert into #column_priv1
  910.             select
  911.                 c.name,
  912.                 u.uid,
  913.                 @owner_uid,
  914.                 0,
  915.                 1,
  916.                 0,
  917.                 1,
  918.                 0,
  919.                 1,
  920.                 0,
  921.                 1,
  922.                 @owner_uid,
  923.                 0
  924.             from syscolumns c, sysusers u
  925.             where id = @table_id
  926.                 and c.number = 0
  927.                 and u.suid = 1        /* grantor is dbo of database */
  928.     end
  929.     else
  930.     begin
  931.         insert into #column_priv1
  932.             select
  933.                 c.name,
  934.                 u.uid,
  935.                 @owner_uid,
  936.                 0,
  937.                 1,
  938.                 0,
  939.                 1,
  940.                 0,
  941.                 1,
  942.                 0,
  943.                 0,
  944.                 @owner_uid,
  945.                 0
  946.             from syscolumns c, sysusers u
  947.             where id = @table_id
  948.                 and c.number = 0
  949.                 and u.suid = 1        /* grantor is dbo of database */
  950.     end
  951. /*
  952. ** now stick in a row for every column for every user in the database
  953. ** we will need to weed out those who have no permissions later
  954. ** (and yes this is a cartesion product: the uid field in sysprotects
  955. ** can also have a group id, in which case we need to extend those
  956. ** privileges to all group members).
  957. */
  958.  
  959.     insert into #column_priv1
  960.         select distinct
  961.             c.name,
  962.             o.uid,
  963.             u.uid,
  964.             0,
  965.             0,
  966.             0,
  967.             0,
  968.             0,
  969.             0,
  970.             0,
  971.             0,
  972.             u.uid,
  973.             u.gid
  974.         from sysusers u, syscolumns c, sysobjects o
  975.         where o.id = @table_id
  976.             and c.id = o.id
  977.             and c.number = 0
  978.             and u.gid <> u.uid
  979.             and u.uid <> @owner_uid
  980.  
  981.     /*
  982.     ** we need to create another temporary table to contain all the various
  983.     ** protection information for the table in question
  984.     */
  985.     create table #protects (
  986.                 uid         smallint NOT NULL,
  987.                 grantor        smallint NOT NULL,
  988.                 action        tinyint NOT NULL,
  989.                 protecttype tinyint NOT NULL,
  990.                 name        varchar(32) NOT NULL)
  991.  
  992.     insert into #protects
  993.         select
  994.             p.uid,
  995.             p.uid,
  996.             p.action,
  997.             p.protecttype,
  998.             isnull(col_name(id, c.number), '~All')
  999.             from
  1000.                 sysprotects p,
  1001.                 master.dbo.spt_values c,
  1002.                 master.dbo.spt_values a,
  1003.                 master.dbo.spt_values b
  1004.             where
  1005.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1006.                     & c.high <> 0
  1007.                     and c.number <= (
  1008.                         select count(*)
  1009.                         from syscolumns
  1010.                         where id = @table_id)
  1011.                 and c.type = 'P'
  1012.                 and a.type = 'T'
  1013.                 and a.number = p.action
  1014.                 and p.action in (193,195,197,26)
  1015.                 and b.type = 'T'
  1016.                 and b.number = p.protecttype
  1017.                 and p.id = @table_id
  1018.                 and p.uid between @low and @high
  1019.  
  1020.  
  1021.     update #column_priv1
  1022.     set select_privilege = 1
  1023.         from #protects p
  1024.     where
  1025.         p.protecttype = 205
  1026.         and p.action = 193
  1027.         and (p.name = #column_priv1.COLUMN_NAME
  1028.             or name = '~All')
  1029.         and (p.uid = 0
  1030.             or p.uid = #column_priv1.gid
  1031.             or p.uid = #column_priv1.uid)
  1032.         and not exists (
  1033.             select * from #protects
  1034.             where
  1035.                 protecttype = 206
  1036.                 and action = 193
  1037.                 and (name = #column_priv1.COLUMN_NAME
  1038.                     or name = '~All')
  1039.                 and ( uid = 0
  1040.                     or uid = #column_priv1.gid
  1041.                     or uid = #column_priv1.uid))
  1042.  
  1043.     update #column_priv1
  1044.     set insert_privilege = 1
  1045.         from #protects p
  1046.     where
  1047.         p.protecttype = 205
  1048.         and p.action = 195
  1049.         and (p.name = #column_priv1.COLUMN_NAME
  1050.             or name = '~All')
  1051.         and (p.uid = 0
  1052.             or p.uid = #column_priv1.gid
  1053.             or p.uid = #column_priv1.uid)
  1054.         and not exists (
  1055.             select * from #protects
  1056.             where
  1057.                 protecttype = 206
  1058.                 and action = 195
  1059.                 and (name = #column_priv1.COLUMN_NAME
  1060.                        or name = '~All')
  1061.                 and (uid = 0
  1062.                     or uid = #column_priv1.gid
  1063.                     or uid = #column_priv1.uid))
  1064.  
  1065.     update #column_priv1
  1066.     set update_privilege = 1
  1067.         from #protects p
  1068.     where
  1069.         p.protecttype = 205
  1070.         and p.action = 197
  1071.         and (p.name = #column_priv1.COLUMN_NAME
  1072.             or name = '~All')
  1073.         and (p.uid = 0
  1074.             or p.uid = #column_priv1.gid
  1075.             or p.uid = #column_priv1.uid)
  1076.         and not exists (
  1077.             select * from #protects
  1078.                 where protecttype = 206
  1079.                 and action = 197
  1080.                 and (name = #column_priv1.COLUMN_NAME
  1081.                     or name = '~All')
  1082.                 and (uid = 0
  1083.                     or uid = #column_priv1.gid
  1084.                     or uid = #column_priv1.uid))
  1085.  
  1086.     update #column_priv1
  1087.     set references_privilege = 1
  1088.         from #protects p
  1089.     where
  1090.         p.protecttype = 205
  1091.         and p.action = 26
  1092.         and (p.name = #column_priv1.COLUMN_NAME
  1093.             or name = '~All')
  1094.         and (p.uid = 0
  1095.             or p.uid = #column_priv1.gid
  1096.             or p.uid = #column_priv1.uid)
  1097.         and not exists (
  1098.             select * from #protects
  1099.                 where protecttype = 206
  1100.                 and action = 26
  1101.                 and (name = #column_priv1.COLUMN_NAME
  1102.                     or name = '~All')
  1103.                 and (uid = 0
  1104.                     or uid = #column_priv1.gid
  1105.                     or uid = #column_priv1.uid))
  1106.  
  1107.     update #column_priv1
  1108.     set select_grantable = 1
  1109.         from #protects p
  1110.     where
  1111.         p.protecttype = 204
  1112.         and p.action = 193
  1113.         and (p.name = #column_priv1.COLUMN_NAME
  1114.             or name = '~All')
  1115.         and (p.uid = 0
  1116.             or p.uid = #column_priv1.gid
  1117.             or p.uid = #column_priv1.uid)
  1118.         and not exists (
  1119.             select * from #protects
  1120.             where
  1121.                 protecttype = 206
  1122.                 and action = 193
  1123.                 and (name = #column_priv1.COLUMN_NAME
  1124.                     or name = '~All')
  1125.                 and ( uid = 0
  1126.                     or uid = #column_priv1.gid
  1127.                     or uid = #column_priv1.uid))
  1128.  
  1129.     update #column_priv1
  1130.     set insert_grantable = 1
  1131.         from #protects p
  1132.     where
  1133.         p.protecttype = 204
  1134.         and p.action = 195
  1135.         and (p.name = #column_priv1.COLUMN_NAME
  1136.             or name = '~All')
  1137.         and (p.uid = 0
  1138.             or p.uid = #column_priv1.gid
  1139.             or p.uid = #column_priv1.uid)
  1140.         and not exists (
  1141.             select * from #protects
  1142.             where
  1143.                 protecttype = 206
  1144.                 and action = 195
  1145.                 and (name = #column_priv1.COLUMN_NAME
  1146.                     or name = '~All')
  1147.                 and ( uid = 0
  1148.                     or uid = #column_priv1.gid
  1149.                     or uid = #column_priv1.uid))
  1150.  
  1151.     update #column_priv1
  1152.     set update_grantable = 1
  1153.         from #protects p
  1154.     where
  1155.         p.protecttype = 204
  1156.         and p.action = 197
  1157.         and (p.name = #column_priv1.COLUMN_NAME
  1158.             or name = '~All')
  1159.         and (p.uid = 0
  1160.             or p.uid = #column_priv1.gid
  1161.             or p.uid = #column_priv1.uid)
  1162.         and not exists (
  1163.             select * from #protects
  1164.             where
  1165.                 protecttype = 206
  1166.                 and action = 197
  1167.                 and (name = #column_priv1.COLUMN_NAME
  1168.                     or name = '~All')
  1169.                 and ( uid = 0
  1170.                     or uid = #column_priv1.gid
  1171.                     or uid = #column_priv1.uid))
  1172.  
  1173.     update #column_priv1
  1174.     set references_grantable = 1
  1175.         from #protects p
  1176.     where
  1177.         p.protecttype = 204
  1178.         and p.action = 26
  1179.         and (p.name = #column_priv1.COLUMN_NAME
  1180.             or name = '~All')
  1181.         and (p.uid = 0
  1182.             or p.uid = #column_priv1.gid
  1183.             or p.uid = #column_priv1.uid)
  1184.         and not exists (
  1185.             select * from #protects
  1186.             where
  1187.                 protecttype = 206
  1188.                 and action = 26
  1189.                 and (name = #column_priv1.COLUMN_NAME
  1190.                     or name = '~All')
  1191.                 and ( uid = 0
  1192.                     or uid = #column_priv1.gid
  1193.                     or uid = #column_priv1.uid))
  1194.  
  1195.     create table #column_priv2(
  1196.         COLUMN_NAME     varchar(32) NOT NULL,
  1197.         grantor         smallint NULL,
  1198.         grantee         smallint NOT NULL,
  1199.         PRIVILEGE        varchar(32) NOT NULL,
  1200.         IS_GRANTABLE    varchar(3) NULL)
  1201.  
  1202.     insert into #column_priv2
  1203.         select
  1204.             COLUMN_NAME,
  1205.             grantor,
  1206.             grantee,
  1207.             'SELECT',
  1208.             'NO'
  1209.         from #column_priv1
  1210.         where select_privilege = 1 and select_grantable    = 0
  1211.  
  1212.     insert into #column_priv2
  1213.         select
  1214.             COLUMN_NAME,
  1215.             grantor,
  1216.             grantee,
  1217.             'INSERT',
  1218.             'NO'
  1219.         from #column_priv1
  1220.         where insert_privilege = 1 and insert_grantable = 0
  1221.  
  1222.     insert into #column_priv2
  1223.         select
  1224.             COLUMN_NAME,
  1225.             grantor,
  1226.             grantee,
  1227.             'UPDATE',
  1228.             'NO'
  1229.         from #column_priv1
  1230.         where update_privilege = 1 and update_grantable = 0
  1231.  
  1232.     insert into #column_priv2
  1233.         select
  1234.             COLUMN_NAME,
  1235.             grantor,
  1236.             grantee,
  1237.             'REFERENCES',
  1238.             'NO'
  1239.         from #column_priv1
  1240.         where references_privilege = 1 and references_grantable = 0
  1241.  
  1242.     insert into #column_priv2
  1243.         select
  1244.             COLUMN_NAME,
  1245.             grantor,
  1246.             grantee,
  1247.             'SELECT',
  1248.             'YES'
  1249.         from #column_priv1
  1250.         where select_grantable = 1
  1251.  
  1252.     insert into #column_priv2
  1253.         select
  1254.             COLUMN_NAME,
  1255.             grantor,
  1256.             grantee,
  1257.             'INSERT',
  1258.             'YES'
  1259.         from #column_priv1
  1260.         where insert_grantable = 1
  1261.  
  1262.     insert into #column_priv2
  1263.         select
  1264.             COLUMN_NAME,
  1265.             grantor,
  1266.             grantee,
  1267.             'UPDATE',
  1268.             'YES'
  1269.         from #column_priv1
  1270.         where update_grantable = 1
  1271.  
  1272.     insert into #column_priv2
  1273.         select
  1274.             COLUMN_NAME,
  1275.             grantor,
  1276.             grantee,
  1277.             'REFERENCES',
  1278.             'YES'
  1279.         from #column_priv1
  1280.         where references_grantable = 1
  1281.  
  1282.     select
  1283.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1284.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1285.         @table_name TABLE_NAME,
  1286.         COLUMN_NAME,
  1287.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1288.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1289.         PRIVILEGE,
  1290.         IS_GRANTABLE
  1291.     from #column_priv2
  1292.     where COLUMN_NAME like @column_name
  1293.     order by 4, 7
  1294. go
  1295.  
  1296. if (charindex('6.50', @@version) = 0 and
  1297.     charindex('7.00', @@version) = 0)
  1298. begin
  1299.     print ''
  1300.     print ''
  1301.     print 'Warning:'
  1302.     print 'you are installing the stored procedures '
  1303.     print 'on a pre 6.50 SQL Server.'
  1304.     print 'Ignore the following errors.'
  1305. end
  1306. else
  1307.     drop proc sp_column_privileges
  1308. go
  1309.  
  1310. /*    Procedure for 6.50 server */
  1311. CREATE PROCEDURE sp_column_privileges (
  1312.             @table_name         varchar(32),
  1313.             @table_owner        varchar(32) = null,
  1314.             @table_qualifier    varchar(32) = null,
  1315.             @column_name        varchar(96) = null)    /* 3*32 */
  1316. as
  1317.  
  1318.     declare @table_id     int
  1319.  
  1320.     if @column_name is null /*    If column name not supplied, match all */
  1321.         select @column_name = '%'
  1322.  
  1323.     if @table_qualifier is not null
  1324.     begin
  1325.         if db_name() <> @table_qualifier
  1326.         begin    /* If qualifier doesn't match current database */
  1327.             raiserror (15250, -1,-1)
  1328.             return
  1329.         end
  1330.     end
  1331.     if @table_owner is null
  1332.     begin    /* If unqualified table name */
  1333.         select @table_id = object_id(@table_name)
  1334.     end
  1335.     else
  1336.     begin    /* Qualified table name */
  1337.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1338.     end
  1339.  
  1340.     select
  1341.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1342.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1343.         @table_name TABLE_NAME,
  1344.         convert(varchar(32),c.name) COLUMN_NAME,
  1345.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1346.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1347.         convert (varchar(32),case p.action
  1348.              when 193 then 'SELECT'
  1349.              when 195 then 'INSERT'
  1350.              when 197 then 'UPDATE'
  1351.              else 'REFERENCES'
  1352.         end) PRIVILEGE,
  1353.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1354.             else 'YES'
  1355.         end) IS_GRANTABLE
  1356.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1357.     where
  1358.         c.id = @table_id
  1359.         and c.name like @column_name
  1360.         and c.id = p.id
  1361.         and c.id = o.id
  1362.         and case substring(p.columns, 1, 1) & 1
  1363.                 when NULL then 255    /* all columns have permission */
  1364.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1365.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1366.             end
  1367.             & v.high <> 0            /* permission applies to this column */
  1368.         and v.number <= (select count(*) from syscolumns
  1369.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1370.         and v.type = 'P'
  1371.         and v.number = c.colid
  1372.             /* expand groups */
  1373.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1374.              (p.uid = u.gid and u.uid <> u.gid))
  1375.         and p.protecttype <> 206    /* only grant rows */
  1376.         and p.action in (26,193,195,197)
  1377.         and o.uid <> u.uid            /* no rows for owner */
  1378.         and not exists (            /* exclude revoke'd privileges */
  1379.             select *
  1380.             from sysprotects p1
  1381.             where
  1382.                 p1.protecttype = 206
  1383.                 and p1.action = p.action
  1384.                 and p1.id = p.id
  1385.                 and p1.uid = u.uid
  1386.                 and case substring(p1.columns, 1, 1) & 1
  1387.                         when NULL then 255    /* all columns have permission */
  1388.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1389.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1390.                     end
  1391.                     & v.high <> 0)            /* permission applies to this column */
  1392.     union all
  1393.     select    /*    Add rows for table owner */
  1394.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1395.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1396.         @table_name TABLE_NAME,
  1397.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1398.         convert(varchar(32),user_name(u.uid)) grantor,
  1399.         convert(varchar(32),user_name(o.uid)) grantee,
  1400.         convert (varchar(32),case v.number
  1401.             when 193 then 'SELECT'
  1402.             when 195 then 'INSERT'
  1403.             when 197 then 'UPDATE'
  1404.             else 'REFERENCES'
  1405.         end) PRIVILEGE,
  1406.         convert(varchar(3),'YES') IS_GRANTABLE
  1407.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1408.     where
  1409.         c.id = @table_id
  1410.         and c.name like @column_name
  1411.         and c.id = o.id
  1412.         and u.suid = 1        /* grantor is dbo of database */
  1413.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1414.         and v.number in (26,193,195,197)
  1415.         and not exists (    /* exclude revoke'd privileges */
  1416.             select *
  1417.             from sysprotects p1
  1418.             where
  1419.                 p1.protecttype = 206
  1420.                 and p1.action = v.number
  1421.                 and p1.id = o.id
  1422.                 and p1.uid = o.uid)
  1423.     order by 4, 7
  1424. go
  1425.  
  1426.  
  1427.  
  1428. if (charindex('7.00', @@version) = 0)
  1429. begin
  1430.     print ''
  1431.     print ''
  1432.     print 'Warning:'
  1433.     print 'you are installing the stored procedures '
  1434.     print 'on a pre 7.0 SQL Server.'
  1435.     print 'Ignore the following errors.'
  1436. end
  1437. else
  1438.     drop proc sp_column_privileges
  1439. go
  1440.  
  1441. /*    Procedure for 7.00 server */
  1442. CREATE PROCEDURE sp_column_privileges (
  1443.             @table_name         sysname,
  1444.             @table_owner        sysname = null,
  1445.             @table_qualifier    sysname = null,
  1446.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1447. as
  1448.  
  1449.     declare @table_id     int
  1450.  
  1451.     if @column_name is null /*    If column name not supplied, match all */
  1452.         select @column_name = '%'
  1453.  
  1454.     if @table_qualifier is not null
  1455.     begin
  1456.         if db_name() <> @table_qualifier
  1457.         begin    /* If qualifier doesn't match current database */
  1458.             raiserror (15250, -1,-1)
  1459.             return
  1460.         end
  1461.     end
  1462.     if @table_owner is null
  1463.     begin    /* If unqualified table name */
  1464.         select @table_id = object_id(quotename(@table_name))
  1465.     end
  1466.     else
  1467.     begin    /* Qualified table name */
  1468.         if @table_owner = N''
  1469.         begin    /* If empty owner name */
  1470.             select @table_id = 0
  1471.         end
  1472.         else
  1473.         begin
  1474.             select @table_id = object_id(quotename(@table_owner) +
  1475.                 '.' + quotename(@table_name))
  1476.         end
  1477.     end
  1478.  
  1479.     select
  1480.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1481.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1482.         @table_name TABLE_NAME,
  1483.         convert(sysname,c.name) COLUMN_NAME,
  1484.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1485.         convert(sysname,user_name(u.uid)) GRANTEE,
  1486.         convert (varchar(32),case p.action
  1487.              when 193 then 'SELECT'
  1488.              when 195 then 'INSERT'
  1489.              when 197 then 'UPDATE'
  1490.              else 'REFERENCES'
  1491.         end) PRIVILEGE,
  1492.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1493.             else 'YES'
  1494.         end) IS_GRANTABLE
  1495.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1496.     where
  1497.         c.id = @table_id
  1498.         and c.name like @column_name
  1499.         and c.id = p.id
  1500.         and c.id = o.id
  1501.         and case substring(p.columns, 1, 1) & 1
  1502.                 when NULL then 255    /* all columns have permission */
  1503.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1504.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1505.             end
  1506.             & v.high <> 0            /* permission applies to this column */
  1507.         and v.number <= (select count(*) from syscolumns
  1508.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1509.         and v.type = 'P'
  1510.         and v.number = c.colid
  1511.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1512.         and (u.uid > 0 and u.uid < 16384)
  1513.         and ((p.uid = u.uid) or 
  1514.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1515.         and p.protecttype <> 206    /* only grant rows */
  1516.         and p.action in (26,193,195,197)
  1517.         and o.uid <> u.uid            /* no rows for owner */
  1518.         and not exists (            /* exclude revoke'd privileges */
  1519.             select *
  1520.             from sysprotects p1
  1521.             where
  1522.                 p1.protecttype = 206
  1523.                 and p1.action = p.action
  1524.                 and p1.id = p.id
  1525.                 and p1.uid = u.uid
  1526.                 and case substring(p1.columns, 1, 1) & 1
  1527.                         when NULL then 255    /* all columns have permission */
  1528.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1529.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1530.                     end
  1531.                     & v.high <> 0)            /* permission applies to this column */
  1532.     union all
  1533.     select    /*    Add rows for table owner */
  1534.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1535.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1536.         @table_name TABLE_NAME,
  1537.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1538.         convert(sysname,user_name(u.uid)) grantor,
  1539.         convert(sysname,user_name(o.uid)) grantee,
  1540.         convert (varchar(32),case v.number
  1541.             when 193 then 'SELECT'
  1542.             when 195 then 'INSERT'
  1543.             when 197 then 'UPDATE'
  1544.             else 'REFERENCES'
  1545.         end) PRIVILEGE,
  1546.         convert(varchar(3),'YES') IS_GRANTABLE
  1547.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1548.     where
  1549.         c.id = @table_id
  1550.         and c.name like @column_name
  1551.         and c.id = o.id
  1552.         and u.uid = 1        /* grantor is 'dbo' of database */
  1553.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1554.         and v.number in (26,193,195,197)
  1555.         and not exists (    /* exclude revoke'd privileges */
  1556.             select *
  1557.             from sysprotects p1
  1558.             where
  1559.                 p1.protecttype = 206
  1560.                 and p1.action = v.number
  1561.                 and p1.id = o.id
  1562.                 and p1.uid = o.uid)
  1563.     order by 4, 7
  1564. go
  1565.  
  1566.  
  1567. grant execute on sp_column_privileges to public
  1568. go
  1569.  
  1570. dump tran master with no_log
  1571. go
  1572.  
  1573. print 'creating sp_columns'
  1574. go
  1575.  
  1576. /*    Procedure for pre-6.0 server */
  1577. CREATE PROCEDURE sp_columns (
  1578.                  @table_name        varchar(96),
  1579.                  @table_owner        varchar(96) = null,
  1580.                  @table_qualifier    varchar(32) = null,
  1581.                  @column_name        varchar(96) = null,
  1582.                  @ODBCVer            int = 2)
  1583. AS
  1584.     DECLARE @full_table_name varchar(193)
  1585.     DECLARE @table_id        int
  1586.  
  1587.     /* Declare bindings */
  1588.     DECLARE @TABLEQUALIFIER     varchar(32),
  1589.         @TABLEOWNER     varchar(32),
  1590.         @TABLENAME     varchar(32),
  1591.         @COLUMNNAME     varchar(32),
  1592.         @ORDINALPOSITION int
  1593.  
  1594.     /* Declare tracking variables */
  1595.     DECLARE @currQualifier varchar(32),
  1596.          @currOwner     varchar(32),
  1597.         @currName      varchar(32),
  1598.         @currPos       int
  1599.  
  1600.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  1601.     CREATE TABLE #columns
  1602.     (
  1603.         TABLE_QUALIFIER        varchar(32),
  1604.         TABLE_OWNER        varchar(32),
  1605.         TABLE_NAME        varchar(32),
  1606.         COLUMN_NAME        varchar(32),
  1607.         DATA_TYPE        smallint,
  1608.         TYPE_NAME        varchar(32),            
  1609.         "PRECISION"        int,
  1610.         LENGTH            int,
  1611.         SCALE             smallint        null,
  1612.         RADIX            smallint        null,
  1613.         NULLABLE        smallint,
  1614.         REMARKS            varchar(254)    null,
  1615.         COLUMN_DEF        varchar(255)    null,
  1616.         SQL_DATA_TYPE        smallint,
  1617.         SQL_DATETIME_SUB    smallint    null,
  1618.         CHAR_OCTET_LENGTH    int            null,
  1619.         ORDINAL_POSITION    int,
  1620.         IS_NULLABLE        varchar(254),
  1621.         SS_DATA_TYPE        tinyint
  1622.     )        
  1623.  
  1624.     /* Declare cursor for fixup */
  1625.     DECLARE curColumns CURSOR
  1626.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  1627.         FROM #columns
  1628.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  1629.  
  1630.     if @ODBCVer <> 3
  1631.         select @ODBCVer = 2
  1632.     if @column_name is null /*    If column name not supplied, match all */
  1633.         select @column_name = '%'
  1634.     if @table_qualifier is not null
  1635.     begin
  1636.         if db_name() <> @table_qualifier
  1637.         begin    /* If qualifier doesn't match current database */
  1638.             raiserror 20001 '~~Rush_5~~'
  1639.             return
  1640.         end
  1641.     end
  1642.     if @table_name is null
  1643.     begin    /*    If table name not supplied, match all */
  1644.         select @table_name = '%'
  1645.     end
  1646.     if @table_owner is null
  1647.     begin    /* If unqualified table name */
  1648.         SELECT @full_table_name = @table_name
  1649.     end
  1650.     else
  1651.     begin    /* Qualified table name */
  1652.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1653.     end
  1654.  
  1655.     /*    Get Object ID */
  1656.     SELECT @table_id = object_id(@full_table_name)
  1657.     if ((charindex('%',@full_table_name) = 0) and
  1658.         (charindex('_',@full_table_name) = 0) and
  1659.         @table_id <> 0)
  1660.     begin
  1661.         /* this block is for the case where there is no pattern
  1662.              matching required for the table name */
  1663.  
  1664.         set nocount on
  1665.  
  1666.         /* Store results into the temp table */
  1667.         insert into #columns
  1668.             (
  1669.             TABLE_QUALIFIER,
  1670.             TABLE_OWNER,
  1671.             TABLE_NAME,
  1672.             COLUMN_NAME,
  1673.             DATA_TYPE,
  1674.             TYPE_NAME,            
  1675.             "PRECISION",
  1676.             LENGTH,
  1677.             SCALE,
  1678.             RADIX,
  1679.             NULLABLE,
  1680.             REMARKS,
  1681.             COLUMN_DEF,
  1682.             SQL_DATA_TYPE,
  1683.             SQL_DATETIME_SUB,
  1684.             CHAR_OCTET_LENGTH,
  1685.             ORDINAL_POSITION,
  1686.             IS_NULLABLE,
  1687.             SS_DATA_TYPE
  1688.             )
  1689.         SELECT
  1690.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1691.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1692.             TABLE_NAME = convert(varchar(32),o.name),
  1693.             COLUMN_NAME = convert(varchar(32),c.name),
  1694.             d.DATA_TYPE,
  1695.             TYPE_NAME = t.name,
  1696.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1697.             LENGTH = isnull(d.length, convert(int,c.length)),
  1698.             SCALE = d.numeric_scale,
  1699.             d.RADIX,
  1700.             NULLABLE =    /* set nullability from status flag */
  1701.                 convert(smallint, convert(bit, c.status&8)),
  1702.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1703.             COLUMN_DEF = text,
  1704.             d.SQL_DATA_TYPE,
  1705.             d.SQL_DATETIME_SUB,
  1706.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1707.             ORDINAL_POSITION = convert(int,c.colid),
  1708.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1709.             SS_DATA_TYPE = c.type
  1710.         FROM
  1711.             syscolumns c,
  1712.             sysobjects o,
  1713.             syscomments m,
  1714.             master.dbo.spt_datatype_info d,
  1715.             systypes t
  1716.         WHERE
  1717.             o.id = @table_id
  1718.             AND c.id = o.id
  1719.             AND t.type = d.ss_dtype
  1720.             AND c.length = isnull(d.fixlen, c.length)
  1721.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1722.             AND o.type <> 'P'
  1723.             AND c.usertype = t.usertype
  1724.             AND c.name like @column_name
  1725.             AND c.cdefault *= m.id
  1726.             AND m.colid = 1
  1727.         ORDER BY 17
  1728.     end
  1729.     else
  1730.     begin
  1731.         /* this block is for the case where there IS pattern
  1732.              matching done on the table name */
  1733.         if @table_owner is null /*    If owner not supplied, match all */
  1734.             select @table_owner = '%'
  1735.  
  1736.         set nocount on
  1737.  
  1738.         /* Store results into the temp table */
  1739.         insert into #columns
  1740.             (
  1741.             TABLE_QUALIFIER,
  1742.             TABLE_OWNER,
  1743.             TABLE_NAME,
  1744.             COLUMN_NAME,
  1745.             DATA_TYPE,
  1746.             TYPE_NAME,            
  1747.             "PRECISION",
  1748.             LENGTH,
  1749.             SCALE,
  1750.             RADIX,
  1751.             NULLABLE,
  1752.             REMARKS,
  1753.             COLUMN_DEF,
  1754.             SQL_DATA_TYPE,
  1755.             SQL_DATETIME_SUB,
  1756.             CHAR_OCTET_LENGTH,
  1757.             ORDINAL_POSITION,
  1758.             IS_NULLABLE,
  1759.             SS_DATA_TYPE
  1760.             )
  1761.         SELECT
  1762.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1763.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1764.             TABLE_NAME = convert(varchar(32),o.name),
  1765.             COLUMN_NAME = convert(varchar(32),c.name),
  1766.             d.DATA_TYPE,
  1767.             TYPE_NAME = t.name,
  1768.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1769.             LENGTH = isnull(d.length, convert(int,c.length)),
  1770.             SCALE = d.numeric_scale,
  1771.             d.RADIX,
  1772.             NULLABLE =    /* set nullability from status flag */
  1773.                 convert(smallint, convert(bit, c.status&8)),
  1774.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1775.             COLUMN_DEF = text,
  1776.             d.SQL_DATA_TYPE,
  1777.             d.SQL_DATETIME_SUB,
  1778.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1779.             ORDINAL_POSITION = convert(int,c.colid),
  1780.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1781.             SS_DATA_TYPE = c.type
  1782.         FROM
  1783.             syscolumns c,
  1784.             sysobjects o,
  1785.             syscomments m,
  1786.             master.dbo.spt_datatype_info d,
  1787.             systypes t
  1788.         WHERE
  1789.             o.name like @table_name
  1790.             AND user_name(o.uid) like @table_owner
  1791.             AND o.id = c.id
  1792.             AND t.type = d.ss_dtype
  1793.             AND c.length = isnull(d.fixlen, c.length)
  1794.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1795.             AND o.type <> 'P'
  1796.             AND c.usertype = t.usertype
  1797.             AND c.name like @column_name
  1798.             AND c.cdefault *= m.id
  1799.             AND m.colid = 1
  1800.         ORDER BY 2, 3, 17
  1801.     end
  1802.  
  1803.     /* Fix ordinal positions */
  1804.     OPEN curColumns
  1805.     FETCH NEXT FROM curColumns
  1806.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  1807.     
  1808.     /* Initialize tracking variables */
  1809.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  1810.     WHILE @@FETCH_STATUS = 0
  1811.     BEGIN
  1812.         /* Check to see if the catalog/schema/table */
  1813.         /* has changed since last row               */
  1814.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  1815.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  1816.         BEGIN
  1817.             /* Increment ordinal count */
  1818.             SELECT @currPos = @currPos + 1
  1819.         END
  1820.         ELSE
  1821.         BEGIN
  1822.             /* Set the catalog/schema/table to new values */
  1823.             SELECT @currQualifier = @TABLEQUALIFIER,
  1824.                    @currOwner     = @TABLEOWNER,
  1825.                    @currName      = @TABLENAME,
  1826.                    @currPos       = 1
  1827.         END
  1828.  
  1829.         /* Check for holes in the ordinal position order */
  1830.         IF (@currPos < @ORDINALPOSITION)
  1831.         BEGIN        
  1832.             /* Update the ordinal position */
  1833.             UPDATE #columns
  1834.             SET    ORDINAL_POSITION = @currPos
  1835.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  1836.               AND  TABLE_OWNER      = @TABLEOWNER
  1837.               AND  TABLE_NAME       = @TABLENAME
  1838.               AND  COLUMN_NAME      = @COLUMNNAME
  1839.         END
  1840.  
  1841.         /* Fetch next result */
  1842.         FETCH NEXT FROM curColumns
  1843.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  1844.     END
  1845.  
  1846.     /* Cleanup cursor */
  1847.     CLOSE curColumns
  1848.     DEALLOCATE curColumns
  1849.  
  1850.     set nocount off
  1851.  
  1852.     /* Output the result set */
  1853.     select *
  1854.     from #columns
  1855.     order by 2, 3, 17
  1856. go
  1857.  
  1858. if (charindex('6.00', @@version) = 0 and
  1859.     charindex('6.50', @@version) = 0 and
  1860.     charindex('7.00', @@version) = 0)
  1861. begin
  1862.     print ''
  1863.     print ''
  1864.     print 'Warning:'
  1865.     print 'you are installing the stored procedures '
  1866.     print 'on a pre 6.0 SQL Server.'
  1867.     print 'Ignore the following error.'
  1868. end
  1869. else
  1870.     drop proc sp_columns
  1871. go
  1872.  
  1873. /*    Procedure for 6.0 and 6.50 server */
  1874. CREATE PROCEDURE sp_columns (
  1875.                  @table_name        varchar(96),
  1876.                  @table_owner        varchar(96) = null,
  1877.                  @table_qualifier    varchar(32) = null,
  1878.                  @column_name        varchar(96) = null,
  1879.                  @ODBCVer            int = 2)
  1880. AS
  1881.     DECLARE @full_table_name    varchar(193)
  1882.     DECLARE @table_id int
  1883.  
  1884.     /* Declare bindings */
  1885.     DECLARE @TABLEQUALIFIER     varchar(32),
  1886.         @TABLEOWNER     varchar(32),
  1887.         @TABLENAME     varchar(32),
  1888.         @COLUMNNAME     varchar(32),
  1889.         @ORDINALPOSITION int
  1890.  
  1891.     /* Declare tracking variables */
  1892.     DECLARE @currQualifier varchar(32),
  1893.          @currOwner     varchar(32),
  1894.         @currName      varchar(32),
  1895.         @currPos       int
  1896.  
  1897.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  1898.     CREATE TABLE #columns
  1899.     (
  1900.         TABLE_QUALIFIER        varchar(32),
  1901.         TABLE_OWNER        varchar(32),
  1902.         TABLE_NAME        varchar(32),
  1903.         COLUMN_NAME        varchar(32),
  1904.         DATA_TYPE        smallint,
  1905.         TYPE_NAME        varchar(32),            
  1906.         "PRECISION"        int,
  1907.         LENGTH            int,
  1908.         SCALE             smallint        null,
  1909.         RADIX            smallint        null,
  1910.         NULLABLE        smallint,
  1911.         REMARKS            varchar(254)    null,
  1912.         COLUMN_DEF        varchar(255)    null,
  1913.         SQL_DATA_TYPE        smallint,
  1914.         SQL_DATETIME_SUB    smallint    null,
  1915.         CHAR_OCTET_LENGTH    int            null,
  1916.         ORDINAL_POSITION    int,
  1917.         IS_NULLABLE        varchar(254),
  1918.         SS_DATA_TYPE        tinyint
  1919.     )        
  1920.  
  1921.     /* Declare cursor for fixup */
  1922.     DECLARE curColumns CURSOR
  1923.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  1924.         FROM #columns
  1925.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  1926.  
  1927.     if @ODBCVer <> 3
  1928.         select @ODBCVer = 2
  1929.     if @column_name is null /*    If column name not supplied, match all */
  1930.         select @column_name = '%'
  1931.     if @table_qualifier is not null
  1932.     begin
  1933.         if db_name() <> @table_qualifier
  1934.         begin    /* If qualifier doesn't match current database */
  1935.             raiserror (15250, -1,-1)
  1936.             return
  1937.         end
  1938.     end
  1939.     if @table_name is null
  1940.     begin
  1941.         /* If table name not supplied, match all */
  1942.         select @table_name = '%'
  1943.     end
  1944.     if @table_owner is null
  1945.     begin
  1946.         /* If unqualified table name */
  1947.         SELECT @full_table_name = @table_name
  1948.     end
  1949.     else
  1950.     begin
  1951.         /* Qualified table name */
  1952.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1953.     end
  1954.  
  1955.     /* Get Object ID */
  1956.     SELECT @table_id = object_id(@full_table_name)
  1957.     if ((charindex('%',@full_table_name) = 0) and
  1958.         (charindex('[',@full_table_name) = 0) and
  1959.         (charindex('_',@full_table_name) = 0) and
  1960.         @table_id <> 0)
  1961.     begin
  1962.         /* this block is for the case where there is no pattern
  1963.             matching required for the table name */
  1964.  
  1965.         set nocount on
  1966.  
  1967.         /* Store results into the temp table */
  1968.         insert into #columns
  1969.             (
  1970.             TABLE_QUALIFIER,
  1971.             TABLE_OWNER,
  1972.             TABLE_NAME,
  1973.             COLUMN_NAME,
  1974.             DATA_TYPE,
  1975.             TYPE_NAME,            
  1976.             "PRECISION",
  1977.             LENGTH,
  1978.             SCALE,
  1979.             RADIX,
  1980.             NULLABLE,
  1981.             REMARKS,
  1982.             COLUMN_DEF,
  1983.             SQL_DATA_TYPE,
  1984.             SQL_DATETIME_SUB,
  1985.             CHAR_OCTET_LENGTH,
  1986.             ORDINAL_POSITION,
  1987.             IS_NULLABLE,
  1988.             SS_DATA_TYPE
  1989.             )
  1990.         SELECT
  1991.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1992.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1993.             TABLE_NAME = convert(varchar(32),o.name),
  1994.             COLUMN_NAME = convert(varchar(32),c.name),
  1995.             DATA_TYPE = d.DATA_TYPE,
  1996.             TYPE_NAME = convert(varchar(32),
  1997.                     case
  1998.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1999.                         else d.TYPE_NAME
  2000.                     end),
  2001.             "PRECISION" = convert(int,
  2002.                     case
  2003.                         when d.DATA_TYPE in (6,7) then d.data_precision    /* FLOAT/REAL */
  2004.                         else isnull(convert(int,c.prec), 2147483647)
  2005.                     end),
  2006.             LENGTH = convert(int,
  2007.                     case
  2008.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2009.                         c.prec+2
  2010.                         else
  2011.                             isnull(d.length, c.length)
  2012.                     end),
  2013.             SCALE = convert(smallint, c.scale),
  2014.             RADIX = d.RADIX,
  2015.             NULLABLE =    /* set nullability from status flag */
  2016.                 convert(smallint, convert(bit, c.status&8)),
  2017.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2018.             COLUMN_DEF = text,
  2019.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2020.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2021.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2022.             ORDINAL_POSITION = convert(int,c.colid),
  2023.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2024.             SS_DATA_TYPE = c.type
  2025.         FROM
  2026.             syscolumns c,
  2027.             sysobjects o,
  2028.             syscomments m,
  2029.             master.dbo.spt_datatype_info d,
  2030.             systypes t
  2031.         WHERE
  2032.             o.id = @table_id
  2033.             AND c.id = o.id
  2034.             AND t.type = d.ss_dtype
  2035.             AND c.length = isnull(d.fixlen, c.length)
  2036.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2037.             AND o.type <> 'P'
  2038.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2039.             AND c.usertype = t.usertype
  2040.             AND c.name like @column_name
  2041.             AND c.cdefault *= m.id
  2042.             AND m.colid = 1
  2043.         ORDER BY 17
  2044.     end
  2045.     else
  2046.     begin
  2047.         /* this block is for the case where there IS pattern
  2048.             matching done on the table name */
  2049.  
  2050.         set nocount on
  2051.  
  2052.         if @table_owner is null /*    If owner not supplied, match all */
  2053.             select @table_owner = '%'
  2054.  
  2055.         /* Store results into the temp table */
  2056.         insert into #columns
  2057.             (
  2058.             TABLE_QUALIFIER,
  2059.             TABLE_OWNER,
  2060.             TABLE_NAME,
  2061.             COLUMN_NAME,
  2062.             DATA_TYPE,
  2063.             TYPE_NAME,            
  2064.             "PRECISION",
  2065.             LENGTH,
  2066.             SCALE,
  2067.             RADIX,
  2068.             NULLABLE,
  2069.             REMARKS,
  2070.             COLUMN_DEF,
  2071.             SQL_DATA_TYPE,
  2072.             SQL_DATETIME_SUB,
  2073.             CHAR_OCTET_LENGTH,
  2074.             ORDINAL_POSITION,
  2075.             IS_NULLABLE,
  2076.             SS_DATA_TYPE
  2077.             )
  2078.         SELECT
  2079.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2080.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2081.             TABLE_NAME = convert(varchar(32),o.name),
  2082.             COLUMN_NAME = convert(varchar(32),c.name),
  2083.             DATA_TYPE = d.DATA_TYPE,
  2084.             TYPE_NAME = convert(varchar(32), 
  2085.                 case 
  2086.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2087.                     else d.TYPE_NAME
  2088.                 end),
  2089.             "PRECISION" = convert(int,
  2090.                     case
  2091.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2092.                         else isnull(convert(int,c.prec), 2147483647)
  2093.                     end),
  2094.             LENGTH = convert(int,
  2095.                     case
  2096.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2097.                             c.prec+2
  2098.                         else
  2099.                             isnull(d.length, c.length)
  2100.                     end),
  2101.             SCALE = convert(smallint, c.scale),
  2102.             RADIX = d.RADIX,
  2103.             NULLABLE =    /* set nullability from status flag */
  2104.                 convert(smallint, convert(bit, c.status&8)),
  2105.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2106.             COLUMN_DEF = text,
  2107.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2108.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2109.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2110.             ORDINAL_POSITION = convert(int,c.colid),
  2111.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2112.             SS_DATA_TYPE = c.type
  2113.         FROM
  2114.             syscolumns c,
  2115.             sysobjects o,
  2116.             syscomments m,
  2117.             master.dbo.spt_datatype_info d,
  2118.             systypes t
  2119.         WHERE
  2120.             o.name like @table_name
  2121.             AND user_name(o.uid) like @table_owner
  2122.             AND o.id = c.id
  2123.             AND t.type = d.ss_dtype
  2124.             AND c.length = isnull(d.fixlen, c.length)
  2125.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2126.             AND o.type <> 'P'
  2127.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2128.             AND c.usertype = t.usertype
  2129.             AND c.name like @column_name
  2130.             AND c.cdefault *= m.id
  2131.             AND m.colid = 1
  2132.         ORDER BY 2, 3, 17
  2133.  
  2134.     end
  2135.  
  2136.     /* Fix ordinal positions */
  2137.     OPEN curColumns
  2138.     FETCH NEXT FROM curColumns
  2139.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2140.     
  2141.     /* Initialize tracking variables */
  2142.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  2143.     WHILE @@FETCH_STATUS = 0
  2144.     BEGIN
  2145.         /* Check to see if the catalog/schema/table */
  2146.         /* has changed since last row               */
  2147.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  2148.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  2149.         BEGIN
  2150.             /* Increment ordinal count */
  2151.             SELECT @currPos = @currPos + 1
  2152.         END
  2153.         ELSE
  2154.         BEGIN
  2155.             /* Set the catalog/schema/table to new values */
  2156.             SELECT @currQualifier = @TABLEQUALIFIER,
  2157.                    @currOwner     = @TABLEOWNER,
  2158.                    @currName      = @TABLENAME,
  2159.                    @currPos       = 1
  2160.         END
  2161.  
  2162.         /* Check for holes in the ordinal position order */
  2163.         IF (@currPos < @ORDINALPOSITION)
  2164.         BEGIN        
  2165.             /* Update the ordinal position */
  2166.             UPDATE #columns
  2167.             SET    ORDINAL_POSITION = @currPos
  2168.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  2169.               AND  TABLE_OWNER      = @TABLEOWNER
  2170.               AND  TABLE_NAME       = @TABLENAME
  2171.               AND  COLUMN_NAME      = @COLUMNNAME
  2172.         END
  2173.  
  2174.         /* Fetch next result */
  2175.         FETCH NEXT FROM curColumns
  2176.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2177.     END
  2178.  
  2179.     /* Cleanup cursor */
  2180.     CLOSE curColumns
  2181.     DEALLOCATE curColumns
  2182.  
  2183.     set nocount off
  2184.  
  2185.     /* Output the result set */
  2186.     select *
  2187.     from #columns
  2188.     order by 2, 3, 17
  2189. go
  2190.  
  2191. if (charindex('7.00', @@version) = 0)
  2192. begin
  2193.     print ''
  2194.     print ''
  2195.     print 'Warning:'
  2196.     print 'you are installing the stored procedures '
  2197.     print 'on a pre 7.0 SQL Server.'
  2198.     print 'Ignore the following errors.'
  2199. end
  2200. else
  2201.     drop proc sp_columns
  2202. go
  2203.  
  2204. /*    Procedure for 7.0 server */
  2205. CREATE PROCEDURE sp_columns (
  2206.                  @table_name        nvarchar(384),
  2207.                  @table_owner        nvarchar(384) = null,
  2208.                  @table_qualifier    sysname = null,
  2209.                  @column_name        nvarchar(384) = null,
  2210.                  @ODBCVer            int = 2)
  2211. AS
  2212.     DECLARE @full_table_name    nvarchar(769)
  2213.     DECLARE @table_id int
  2214.  
  2215.     /* Declare bindings */
  2216.     DECLARE @TABLEQUALIFIER     sysname,
  2217.         @TABLEOWNER     sysname,
  2218.         @TABLENAME     sysname,
  2219.         @COLUMNNAME     sysname,
  2220.         @ORDINALPOSITION int
  2221.  
  2222.     /* Declare tracking variables */
  2223.     DECLARE @currQualifier varchar(32),
  2224.          @currOwner     varchar(32),
  2225.         @currName      varchar(32),
  2226.         @currPos       int
  2227.  
  2228.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  2229.     CREATE TABLE #columns
  2230.     (
  2231.         TABLE_QUALIFIER        sysname,
  2232.         TABLE_OWNER        sysname,
  2233.         TABLE_NAME        sysname,
  2234.         COLUMN_NAME        sysname,
  2235.         DATA_TYPE        smallint,
  2236.         TYPE_NAME        sysname,
  2237.         "PRECISION"        int,
  2238.         LENGTH            int,
  2239.         SCALE             smallint        null,
  2240.         RADIX            smallint        null,
  2241.         NULLABLE        smallint,
  2242.         REMARKS            varchar(254)    null,
  2243.         COLUMN_DEF        ntext            null,
  2244.         SQL_DATA_TYPE        smallint,
  2245.         SQL_DATETIME_SUB    smallint    null,
  2246.         CHAR_OCTET_LENGTH    int            null,
  2247.         ORDINAL_POSITION    int,
  2248.         IS_NULLABLE        varchar(254),
  2249.         SS_DATA_TYPE        tinyint
  2250.     )        
  2251.  
  2252.     /* Declare cursor for fixup */
  2253.     DECLARE curColumns CURSOR
  2254.     FOR SELECT TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  2255.         FROM #columns
  2256.         ORDER BY TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, ORDINAL_POSITION
  2257.     if @ODBCVer <> 3
  2258.         select @ODBCVer = 2
  2259.     if @column_name is null /*    If column name not supplied, match all */
  2260.         select @column_name = '%'
  2261.     if @table_qualifier is not null
  2262.     begin
  2263.         if db_name() <> @table_qualifier
  2264.         begin    /* If qualifier doesn't match current database */
  2265.             raiserror (15250, -1,-1)
  2266.             return
  2267.         end
  2268.     end
  2269.     if @table_name is null
  2270.     begin    /*    If table name not supplied, match all */
  2271.         select @table_name = '%'
  2272.     end
  2273.     if @table_owner is null
  2274.     begin    /* If unqualified table name */
  2275.         SELECT @full_table_name = quotename(@table_name)
  2276.     end
  2277.     else
  2278.     begin    /* Qualified table name */
  2279.         if @table_owner = ''
  2280.         begin    /* If empty owner name */
  2281.             SELECT @full_table_name = quotename(@table_owner)
  2282.         end
  2283.         else
  2284.         begin
  2285.             SELECT @full_table_name = quotename(@table_owner) +
  2286.                 '.' + quotename(@table_name)
  2287.         end
  2288.     end
  2289.  
  2290.     /*    Get Object ID */
  2291.     SELECT @table_id = object_id(@full_table_name)
  2292.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2293.         (isnull(charindex('[', @table_name),0) = 0) and
  2294.         (isnull(charindex('[', @table_owner),0) = 0) and
  2295.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2296.         @table_id <> 0)
  2297.     begin
  2298.         /* this block is for the case where there is no pattern
  2299.             matching required for the table name */
  2300.         
  2301.         set nocount on
  2302.  
  2303.         /* Store results into the temp table */
  2304.         insert into #columns
  2305.             (
  2306.             TABLE_QUALIFIER,
  2307.             TABLE_OWNER,
  2308.             TABLE_NAME,
  2309.             COLUMN_NAME,
  2310.             DATA_TYPE,
  2311.             TYPE_NAME,            
  2312.             "PRECISION",
  2313.             LENGTH,
  2314.             SCALE,
  2315.             RADIX,
  2316.             NULLABLE,
  2317.             REMARKS,
  2318.             COLUMN_DEF,
  2319.             SQL_DATA_TYPE,
  2320.             SQL_DATETIME_SUB,
  2321.             CHAR_OCTET_LENGTH,
  2322.             ORDINAL_POSITION,
  2323.             IS_NULLABLE,
  2324.             SS_DATA_TYPE
  2325.             )
  2326.         SELECT
  2327.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2328.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2329.             TABLE_NAME = convert(sysname,o.name),
  2330.             COLUMN_NAME = convert(sysname,c.name),
  2331.             d.DATA_TYPE,
  2332.             convert (sysname,case
  2333.                 when t.xusertype > 255 then t.name
  2334.                 else d.TYPE_NAME
  2335.             end) TYPE_NAME,
  2336.             convert(int,case
  2337.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2338.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2339.             end) "PRECISION",
  2340.             convert(int,case
  2341.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2342.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2343.                 else
  2344.                     isnull(d.length, c.length)
  2345.             end) LENGTH,
  2346.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2347.             d.RADIX,
  2348.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2349.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2350.             COLUMN_DEF = text,
  2351.             d.SQL_DATA_TYPE,
  2352.             d.SQL_DATETIME_SUB,
  2353.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2354.             ORDINAL_POSITION = convert(int,c.colid),
  2355.             IS_NULLABLE = convert(varchar(254),
  2356.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2357.             SS_DATA_TYPE = c.type
  2358.         FROM
  2359.             sysobjects o,
  2360.             master.dbo.spt_datatype_info d,
  2361.             systypes t,
  2362.             syscolumns c
  2363.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2364.                 AND m.colid = 1
  2365.         WHERE
  2366.             o.id = @table_id
  2367.             AND c.id = o.id
  2368.             AND t.xtype = d.ss_dtype
  2369.             AND c.length = isnull(d.fixlen, c.length)
  2370.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2371.             AND o.type <> 'P'
  2372.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2373.             AND c.xusertype = t.xusertype
  2374.             AND c.name like @column_name
  2375.         ORDER BY 17
  2376.     end
  2377.     else
  2378.     begin
  2379.         /* this block is for the case where there IS pattern
  2380.             matching done on the table name */
  2381.  
  2382.         if @table_owner is null /*    If owner not supplied, match all */
  2383.             select @table_owner = '%'
  2384.  
  2385.         set nocount on
  2386.  
  2387.         /* Store results into the temp table */
  2388.         insert into #columns
  2389.             (
  2390.             TABLE_QUALIFIER,
  2391.             TABLE_OWNER,
  2392.             TABLE_NAME,
  2393.             COLUMN_NAME,
  2394.             DATA_TYPE,
  2395.             TYPE_NAME,            
  2396.             "PRECISION",
  2397.             LENGTH,
  2398.             SCALE,
  2399.             RADIX,
  2400.             NULLABLE,
  2401.             REMARKS,
  2402.             COLUMN_DEF,
  2403.             SQL_DATA_TYPE,
  2404.             SQL_DATETIME_SUB,
  2405.             CHAR_OCTET_LENGTH,
  2406.             ORDINAL_POSITION,
  2407.             IS_NULLABLE,
  2408.             SS_DATA_TYPE
  2409.             )
  2410.         SELECT
  2411.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2412.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2413.             TABLE_NAME = convert(sysname,o.name),
  2414.             COLUMN_NAME = convert(sysname,c.name),
  2415.             d.DATA_TYPE,
  2416.             convert (sysname,case
  2417.                 when t.xusertype > 255 then t.name
  2418.                 else d.TYPE_NAME
  2419.             end) TYPE_NAME,
  2420.             convert(int,case
  2421.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2422.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2423.             end) "PRECISION",
  2424.             convert(int,case
  2425.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2426.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2427.                 else
  2428.                     isnull(d.length, c.length)
  2429.             end) LENGTH,
  2430.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2431.             d.RADIX,
  2432.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2433.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2434.             COLUMN_DEF = text,
  2435.             d.SQL_DATA_TYPE,
  2436.             d.SQL_DATETIME_SUB,
  2437.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2438.             ORDINAL_POSITION = convert(int,c.colid),
  2439.             IS_NULLABLE = convert(varchar(254),
  2440.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2441.             SS_DATA_TYPE = c.type
  2442.         FROM
  2443.             sysobjects o,
  2444.             master.dbo.spt_datatype_info d,
  2445.             systypes t,
  2446.             syscolumns c
  2447.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2448.                 AND m.colid = 1
  2449.         WHERE
  2450.             o.name like @table_name
  2451.             AND user_name(o.uid) like @table_owner
  2452.             AND o.id = c.id
  2453.             AND t.xtype = d.ss_dtype
  2454.             AND c.length = isnull(d.fixlen, c.length)
  2455.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2456.             AND o.type <> 'P'
  2457.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2458.             AND c.xusertype = t.xusertype
  2459.             AND c.name like @column_name
  2460.         ORDER BY 2, 3, 17
  2461.     end
  2462.     /* Fix ordinal positions */
  2463.     OPEN curColumns
  2464.     FETCH NEXT FROM curColumns
  2465.     INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2466.     
  2467.     /* Initialize tracking variables */
  2468.     SELECT @currQualifier = NULL, @currOwner = NULL, @currName = NULL
  2469.     WHILE @@FETCH_STATUS = 0
  2470.     BEGIN
  2471.         /* Check to see if the catalog/schema/table */
  2472.         /* has changed since last row               */
  2473.         IF (@currQualifier  + '.' + @currOwner  + '.' + @currName =
  2474.             @TABLEQUALIFIER + '.' + @TABLEOWNER + '.' + @TABLENAME)
  2475.         BEGIN
  2476.             /* Increment ordinal count */
  2477.             SELECT @currPos = @currPos + 1
  2478.         END
  2479.         ELSE
  2480.         BEGIN
  2481.             /* Set the catalog/schema/table to new values */
  2482.             SELECT @currQualifier = @TABLEQUALIFIER,
  2483.                    @currOwner     = @TABLEOWNER,
  2484.                    @currName      = @TABLENAME,
  2485.                    @currPos       = 1
  2486.         END
  2487.  
  2488.         /* Check for holes in the ordinal position order */
  2489.         IF (@currPos < @ORDINALPOSITION)
  2490.         BEGIN        
  2491.             /* Update the ordinal position */
  2492.             UPDATE #columns
  2493.             SET    ORDINAL_POSITION = @currPos
  2494.             WHERE  TABLE_QUALIFIER  = @TABLEQUALIFIER
  2495.               AND  TABLE_OWNER      = @TABLEOWNER
  2496.               AND  TABLE_NAME       = @TABLENAME
  2497.               AND  COLUMN_NAME      = @COLUMNNAME
  2498.         END
  2499.  
  2500.         /* Fetch next result */
  2501.         FETCH NEXT FROM curColumns
  2502.         INTO @TABLEQUALIFIER, @TABLEOWNER, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  2503.     END
  2504.  
  2505.     /* Cleanup cursor */
  2506.     CLOSE curColumns
  2507.     DEALLOCATE curColumns
  2508.  
  2509.     set nocount off
  2510.  
  2511.     /* Output the result set */
  2512.     select     TABLE_QUALIFIER,
  2513.         TABLE_OWNER,
  2514.         TABLE_NAME,
  2515.         COLUMN_NAME,
  2516.         DATA_TYPE,
  2517.         TYPE_NAME,
  2518.         "PRECISION",
  2519.         LENGTH,
  2520.         SCALE,
  2521.         RADIX,
  2522.         NULLABLE,
  2523.         REMARKS,
  2524.         COLUMN_DEF = convert(nvarchar(4000), COLUMN_DEF),
  2525.         SQL_DATA_TYPE,
  2526.         SQL_DATETIME_SUB,
  2527.         CHAR_OCTET_LENGTH,
  2528.         ORDINAL_POSITION,
  2529.         IS_NULLABLE,
  2530.         SS_DATA_TYPE
  2531.     from #columns
  2532.     order by 2, 3, 17
  2533. go
  2534.  
  2535. grant execute on sp_columns to public
  2536. go
  2537.  
  2538. dump tran master with no_log
  2539. go
  2540.  
  2541. print 'creating sp_databases'
  2542. go
  2543.  
  2544. /*    Procedure for pre-7.0 server */
  2545. create proc sp_databases
  2546. as
  2547.     set nocount on
  2548.     /* Use temporary table to sum up database size w/o using group by */
  2549.     create table #databases (
  2550.                   DATABASE_NAME varchar(32) NOT NULL,
  2551.                   size int NOT NULL)
  2552.  
  2553.     /* Insert row for each database */
  2554.     insert into #databases
  2555.         select
  2556.             name,
  2557.             (select sum(size) from master.dbo.sysusages
  2558.                 where dbid = d.dbid)
  2559.         from master.dbo.sysdatabases d
  2560.  
  2561.     select
  2562.          DATABASE_NAME,
  2563.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2564.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2565.     from #databases
  2566.     order by 1
  2567. go
  2568.  
  2569. if (charindex('7.00', @@version) = 0)
  2570. begin
  2571.     print ''
  2572.     print ''
  2573.     print 'Warning:'
  2574.     print 'you are installing the stored procedures '
  2575.     print 'on a pre 7.0 SQL Server.'
  2576.     print 'Ignore the following errors.'
  2577. end
  2578. else
  2579.     drop proc sp_databases
  2580. go
  2581.  
  2582. /*    Procedure for 7.0 server */
  2583. create proc sp_databases
  2584. as
  2585.     set nocount on
  2586.     declare @name sysname
  2587.     declare @SQL  nvarchar(600)
  2588.  
  2589.     /* Use temporary table to sum up database size w/o using group by */
  2590.     create table #databases (
  2591.                   DATABASE_NAME sysname NOT NULL,
  2592.                   size int NOT NULL)
  2593.  
  2594.     declare c1 cursor for 
  2595.         select name from master.dbo.sysdatabases
  2596.  
  2597.     open c1
  2598.     fetch c1 into @name
  2599.  
  2600.     while @@fetch_status >= 0
  2601.     begin
  2602.         select @SQL = 'insert into #databases
  2603.                 select N'''+ @name + ''', sum(size) from '
  2604.                 + QuoteName(@name) + '.dbo.sysfiles'
  2605.         /* Insert row for each database */
  2606.         execute (@SQL)
  2607.         fetch c1 into @name
  2608.     end
  2609.     deallocate c1
  2610.  
  2611.     select    
  2612.         DATABASE_NAME,
  2613.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2614.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2615.     from #databases
  2616.     order by 1
  2617. go
  2618.  
  2619. grant execute on sp_databases to public
  2620. go
  2621.  
  2622. dump tran master with no_log
  2623. go
  2624.  
  2625. print 'creating sp_datatype_info'
  2626. go
  2627.  
  2628. /*    Procedure for pre-6.0 server */
  2629. create proc sp_datatype_info
  2630.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2631. as
  2632.     if @ODBCVer <> 3
  2633.         select @ODBCVer = 2
  2634.     if @data_type = 0
  2635.         select
  2636.             TYPE_NAME = t.name,
  2637.             d.DATA_TYPE,
  2638.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2639.             d.LITERAL_PREFIX,
  2640.             d.LITERAL_SUFFIX,
  2641.             e.CREATE_PARAMS,
  2642.             d.NULLABLE,
  2643.             d.CASE_SENSITIVE,
  2644.             d.SEARCHABLE,
  2645.             d.UNSIGNED_ATTRIBUTE,
  2646.             d.MONEY,
  2647.             d.AUTO_INCREMENT,
  2648.             LOCAL_TYPE_NAME = t.name,
  2649.             MINIMUM_SCALE = d.numeric_scale,
  2650.             MAXIMUM_SCALE = d.numeric_scale,
  2651.             d.SQL_DATA_TYPE,
  2652.             d.SQL_DATETIME_SUB,
  2653.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2654.             INTERVAL_PRECISION = convert(smallint,NULL),
  2655.             USERTYPE = t.usertype
  2656.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2657.         where
  2658.             d.ss_dtype = t.type
  2659.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2660.             and t.usertype *= e.user_type
  2661.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2662.                 t.usertype > 100)
  2663.         order by 2, 12, 11, t.usertype
  2664.  
  2665.     else
  2666.         select
  2667.             TYPE_NAME = t.name,
  2668.             d.DATA_TYPE,
  2669.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2670.             d.LITERAL_PREFIX,
  2671.             d.LITERAL_SUFFIX,
  2672.             e.CREATE_PARAMS,
  2673.             d.NULLABLE,
  2674.             d.CASE_SENSITIVE,
  2675.             d.SEARCHABLE,
  2676.             d.UNSIGNED_ATTRIBUTE,
  2677.             d.MONEY,
  2678.             d.AUTO_INCREMENT,
  2679.             LOCAL_TYPE_NAME = t.name,
  2680.             MINIMUM_SCALE = d.numeric_scale,
  2681.             MAXIMUM_SCALE = d.numeric_scale,
  2682.             d.SQL_DATA_TYPE,
  2683.             d.SQL_DATETIME_SUB,
  2684.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2685.             INTERVAL_PRECISION = convert(smallint,NULL),
  2686.             USERTYPE = t.usertype
  2687.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2688.         where
  2689.             DATA_TYPE = @data_type
  2690.             and d.ss_dtype = t.type
  2691.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2692.             and t.usertype *= e.user_type
  2693.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2694.                 t.usertype > 100)
  2695.         order by 12, 11, t.usertype
  2696.  
  2697. go
  2698.  
  2699. if (charindex('6.00', @@version) = 0 and
  2700.     charindex('6.50', @@version) = 0 and
  2701.     charindex('7.00', @@version) = 0)
  2702. begin
  2703.     print ''
  2704.     print ''
  2705.     print 'Warning:'
  2706.     print 'you are installing the stored procedures '
  2707.     print 'on a pre 6.0 SQL Server.'
  2708.     print 'Ignore the following errors.'
  2709. end
  2710. else
  2711.     drop proc sp_datatype_info
  2712. go
  2713.  
  2714. /*    Procedure for 6.0 and 6.50 servers */
  2715. create proc sp_datatype_info
  2716.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2717. as
  2718.     if @ODBCVer <> 3
  2719.         select @ODBCVer = 2
  2720.     if @data_type = 0
  2721.         select
  2722.             convert(varchar(32),case
  2723.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2724.                 else d.TYPE_NAME
  2725.             end) TYPE_NAME,
  2726.             d.DATA_TYPE,
  2727.             convert(int,case
  2728.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2729.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2730.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2731.                 else t.prec
  2732.             end) "PRECISION",
  2733.             d.LITERAL_PREFIX,
  2734.             d.LITERAL_SUFFIX,
  2735.             e.CREATE_PARAMS,
  2736.             convert(smallint,case
  2737.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2738.                 else t.allownulls
  2739.             end) NULLABLE,
  2740.             d.CASE_SENSITIVE,
  2741.             d.SEARCHABLE,
  2742.             d.UNSIGNED_ATTRIBUTE,
  2743.             d.MONEY,
  2744.             d.AUTO_INCREMENT,
  2745.             convert(varchar(32),case
  2746.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2747.                 else d.TYPE_NAME
  2748.             end) LOCAL_TYPE_NAME,
  2749.             convert(smallint,case
  2750.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2751.                 else d.numeric_scale
  2752.             end) MINIMUM_SCALE,
  2753.             convert(smallint,case
  2754.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2755.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2756.                 else t.scale
  2757.             end) MAXIMUM_SCALE,
  2758.             d.SQL_DATA_TYPE,
  2759.             d.SQL_DATETIME_SUB,
  2760.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2761.             INTERVAL_PRECISION = convert(smallint,NULL),
  2762.             USERTYPE = t.usertype
  2763.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2764.         where
  2765.             d.ss_dtype = t.type
  2766.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2767.             and t.usertype *= e.user_type
  2768.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2769.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2770.                 t.usertype > 100)
  2771.             and (t.usertype <= 100 or
  2772.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2773.         order by 2, 12, 11, t.usertype
  2774.  
  2775.     else
  2776.         select
  2777.             convert(varchar(32),case
  2778.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2779.                 else d.TYPE_NAME
  2780.             end) TYPE_NAME,
  2781.             d.DATA_TYPE,
  2782.             convert(int,case
  2783.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2784.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2785.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2786.                 else t.prec
  2787.             end) "PRECISION",
  2788.             d.LITERAL_PREFIX,
  2789.             d.LITERAL_SUFFIX,
  2790.             e.CREATE_PARAMS,
  2791.             convert(smallint,case
  2792.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2793.                 else t.allownulls
  2794.             end) NULLABLE,
  2795.             d.CASE_SENSITIVE,
  2796.             d.SEARCHABLE,
  2797.             d.UNSIGNED_ATTRIBUTE,
  2798.             d.MONEY,
  2799.             d.AUTO_INCREMENT,
  2800.             convert(varchar(32),case
  2801.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2802.                 else d.TYPE_NAME
  2803.             end) LOCAL_TYPE_NAME,
  2804.             convert(smallint,case
  2805.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2806.                 else d.numeric_scale
  2807.             end) MINIMUM_SCALE,
  2808.             convert(smallint,case
  2809.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2810.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2811.                 else t.scale
  2812.             end) MAXIMUM_SCALE,
  2813.             d.SQL_DATA_TYPE,
  2814.             d.SQL_DATETIME_SUB,
  2815.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2816.             INTERVAL_PRECISION = convert(smallint,NULL),
  2817.             USERTYPE = t.usertype
  2818.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2819.         where
  2820.             d.DATA_TYPE = @data_type
  2821.             and d.ss_dtype = t.type
  2822.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2823.             and t.usertype *= e.user_type
  2824.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2825.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2826.                 t.usertype > 100)
  2827.             and (t.usertype <= 100 or
  2828.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2829.         order by 12, 11, t.usertype
  2830. go
  2831.  
  2832. if (charindex('7.00', @@version) = 0)
  2833. begin
  2834.     print ''
  2835.     print ''
  2836.     print 'Warning:'
  2837.     print 'you are installing the stored procedures '
  2838.     print 'on a pre 7.0 SQL Server.'
  2839.     print 'Ignore the following errors.'
  2840. end
  2841. else
  2842.     drop proc sp_datatype_info
  2843. go
  2844.  
  2845. /*    Procedure for 7.0 server */
  2846. create proc sp_datatype_info
  2847.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2848. as
  2849.     declare @mintype int
  2850.     declare @maxtype int
  2851.  
  2852.     if @ODBCVer <> 3
  2853.         select @ODBCVer = 2
  2854.     if @data_type = 0
  2855.     begin
  2856.         select @mintype = -32768
  2857.         select @maxtype = 32767
  2858.     end
  2859.     else
  2860.     begin
  2861.         select @mintype = @data_type
  2862.         select @maxtype = @data_type
  2863.     end
  2864.  
  2865.     select
  2866.         convert(sysname,case
  2867.             when t.xusertype > 255 then t.name
  2868.             else d.TYPE_NAME
  2869.         end) TYPE_NAME,
  2870.         d.DATA_TYPE,
  2871.         convert(int,case
  2872.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2873.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2874.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2875.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2876.         end) "PRECISION",
  2877.         d.LITERAL_PREFIX,
  2878.         d.LITERAL_SUFFIX,
  2879.         e.CREATE_PARAMS,
  2880.         convert(smallint,case
  2881.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2882.             else TypeProperty (t.name, 'AllowsNull')
  2883.         end) NULLABLE,
  2884.         d.CASE_SENSITIVE,
  2885.         d.SEARCHABLE,
  2886.         d.UNSIGNED_ATTRIBUTE,
  2887.         d.MONEY,
  2888.         d.AUTO_INCREMENT,
  2889.         convert(sysname,case
  2890.             when t.xusertype > 255 then t.name
  2891.             else d.LOCAL_TYPE_NAME
  2892.         end) LOCAL_TYPE_NAME,
  2893.         convert(smallint,case
  2894.             when type_name(d.ss_dtype) IN ('numeric','decimal') and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2895.             else d.numeric_scale
  2896.         end) MINIMUM_SCALE,
  2897.         convert(smallint,case
  2898.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2899.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2900.             else TypeProperty (t.name, 'Scale')
  2901.         end) MAXIMUM_SCALE,
  2902.         d.SQL_DATA_TYPE,
  2903.         d.SQL_DATETIME_SUB,
  2904.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2905.         INTERVAL_PRECISION = convert(smallint,NULL),
  2906.         USERTYPE = t.usertype
  2907.     from master.dbo.spt_datatype_info d
  2908.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2909.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2910.             t.xusertype = e.user_type
  2911.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2912.     where
  2913.         d.DATA_TYPE between @mintype and @maxtype
  2914.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2915.         and (t.xusertype <= 255 or
  2916.             isnull(d.AUTO_INCREMENT,0) = 0)
  2917.     order by 2, 12, 11,
  2918.     case
  2919.         when t.usertype=18 then 255
  2920.         else t.usertype
  2921.     end
  2922.  
  2923. go
  2924.  
  2925. grant execute on sp_datatype_info to public
  2926. go
  2927.  
  2928. dump tran master with no_log
  2929. go
  2930.  
  2931. print 'creating sp_fkeys'
  2932. go
  2933.  
  2934. /*    Procedure for pre-6.0 server */
  2935. CREATE PROCEDURE sp_fkeys(
  2936.                @pktable_name        varchar(32) = null,
  2937.                @pktable_owner        varchar(32) = null,
  2938.                @pktable_qualifier    varchar(32) = null,
  2939.                @fktable_name        varchar(32) = null,
  2940.                @fktable_owner        varchar(32) = null,
  2941.                @fktable_qualifier    varchar(32) = null )
  2942. as
  2943.     set nocount on
  2944.     declare    @order_by_pk int
  2945.  
  2946.     select  @order_by_pk = 0
  2947.  
  2948.     if (@pktable_name is null) and (@fktable_name is null)
  2949.     begin    /* If neither primary key nor foreign key table names given */
  2950.         raiserror 20004 '~~Rush_25~~'
  2951.         return
  2952.     end
  2953.     if @fktable_qualifier is not null
  2954.     begin
  2955.         if db_name() <> @fktable_qualifier
  2956.         begin    /* If qualifier doesn't match current database */
  2957.             raiserror 20001 '~~Rush_26~~'
  2958.             return
  2959.         end
  2960.     end
  2961.     if @pktable_qualifier is not null
  2962.     begin
  2963.         if db_name() <> @pktable_qualifier
  2964.         begin    /* If qualifier doesn't match current database */
  2965.             raiserror 20001 '~~Rush_27~~'
  2966.             return
  2967.         end
  2968.     end
  2969.  
  2970.     if @pktable_name is null
  2971.     begin /*  If table name not supplied, match all */
  2972.         select @pktable_name = '%'
  2973.         select @order_by_pk = 1
  2974.     end
  2975.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2976.         select @pktable_owner = '%'
  2977.     if @fktable_name is null    /*    If table name not supplied, match all */
  2978.         select @fktable_name = '%'
  2979.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2980.         select @fktable_owner = '%'
  2981.  
  2982.     if @@trancount <> 0
  2983.     begin    /* If inside a transaction */
  2984.         raiserror 20003 '~~Rush_28~~'
  2985.         return
  2986.     end
  2987.     create table #fkeys(
  2988.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2989.              PKTABLE_OWNER        varchar(32) NULL,
  2990.              PKTABLE_NAME        varchar(32) NOT NULL,
  2991.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2992.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2993.              FKTABLE_OWNER        varchar(32) NULL,
  2994.              FKTABLE_NAME        varchar(32) NOT NULL,
  2995.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2996.              KEY_SEQ            smallint NOT NULL)
  2997.  
  2998.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2999.     /*    Process syskeys for each relationship */
  3000.     /*    The inserts below adds a row to the temp table for each of the
  3001.         8 possible relationships */
  3002.     insert into #fkeys
  3003.         select
  3004.             db_name(),
  3005.             user_name(o1.uid),
  3006.             object_name(k.depid),
  3007.             c2.name,
  3008.             db_name(),
  3009.             user_name(o2.uid),
  3010.             object_name(k.id),
  3011.             c1.name,
  3012.             1
  3013.         from
  3014.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3015.         where
  3016.             c1.id = k.id
  3017.             and k.type = 2    /* Foreign type key */
  3018.             and c1.colid = k.key1
  3019.             and c2.id = k.depid
  3020.             and c2.colid = k.depkey1
  3021.             and o1.id = k.depid
  3022.             and o2.id = k.id
  3023.     union all
  3024.         select
  3025.             db_name(),
  3026.             user_name(o1.uid),
  3027.             object_name(k.depid),
  3028.             c2.name,
  3029.             db_name(),
  3030.             user_name(o2.uid),
  3031.             object_name(k.id),
  3032.             c1.name,
  3033.             2
  3034.         from
  3035.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3036.         where
  3037.             c1.id = k.id
  3038.             and k.type = 2    /* Foreign type key */
  3039.             and c1.colid = k.key2
  3040.             and c2.id = k.depid
  3041.             and c2.colid = k.depkey2
  3042.             and o1.id = k.depid
  3043.             and o2.id = k.id
  3044.     union all
  3045.         select
  3046.             db_name(),
  3047.             user_name(o1.uid),
  3048.             object_name(k.depid),
  3049.             c2.name,
  3050.             db_name(),
  3051.             user_name(o2.uid),
  3052.             object_name(k.id),
  3053.             c1.name,
  3054.             3
  3055.         from
  3056.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3057.         where
  3058.             c1.id = k.id
  3059.             and k.type = 2    /* Foreign type key */
  3060.             and c1.colid = k.key3
  3061.             and c2.id = k.depid
  3062.             and c2.colid = k.depkey3
  3063.             and o1.id = k.depid
  3064.             and o2.id = k.id
  3065.     union all
  3066.         select
  3067.             db_name(),
  3068.             user_name(o1.uid),
  3069.             object_name(k.depid),
  3070.             c2.name,
  3071.             db_name(),
  3072.             user_name(o2.uid),
  3073.             object_name(k.id),
  3074.             c1.name,
  3075.             4
  3076.         from
  3077.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3078.         where
  3079.             c1.id = k.id
  3080.             and k.type = 2    /* Foreign type key */
  3081.             and c1.colid = k.key4
  3082.             and c2.id = k.depid
  3083.             and c2.colid = k.depkey4
  3084.             and o1.id = k.depid
  3085.             and o2.id = k.id
  3086.     union all
  3087.         select
  3088.             db_name(),
  3089.             user_name(o1.uid),
  3090.             object_name(k.depid),
  3091.             c2.name,
  3092.             db_name(),
  3093.             user_name(o2.uid),
  3094.             object_name(k.id),
  3095.             c1.name,
  3096.             5
  3097.         from
  3098.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3099.         where
  3100.             c1.id = k.id
  3101.             and k.type = 2    /* Foreign type key */
  3102.             and c1.colid = k.key5
  3103.             and c2.id = k.depid
  3104.             and c2.colid = k.depkey5
  3105.             and o1.id = k.depid
  3106.             and o2.id = k.id
  3107.     union all
  3108.         select
  3109.             db_name(),
  3110.             user_name(o1.uid),
  3111.             object_name(k.depid),
  3112.             c2.name,
  3113.             db_name(),
  3114.             user_name(o2.uid),
  3115.             object_name(k.id),
  3116.             c1.name,
  3117.             6
  3118.         from
  3119.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3120.         where
  3121.             c1.id = k.id
  3122.             and k.type = 2    /* Foreign type key */
  3123.             and c1.colid = k.key6
  3124.             and c2.id = k.depid
  3125.             and c2.colid = k.depkey6
  3126.             and o1.id = k.depid
  3127.             and o2.id = k.id
  3128.     union all
  3129.         select
  3130.             db_name(),
  3131.             user_name(o1.uid),
  3132.             object_name(k.depid),
  3133.             c2.name,
  3134.             db_name(),
  3135.             user_name(o2.uid),
  3136.             object_name(k.id),
  3137.             c1.name,
  3138.             7
  3139.         from
  3140.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3141.         where
  3142.             c1.id = k.id
  3143.             and k.type = 2    /* Foreign type key */
  3144.             and c1.colid = k.key7
  3145.             and c2.id = k.depid
  3146.             and c2.colid = k.depkey7
  3147.             and o1.id = k.depid
  3148.             and o2.id = k.id
  3149.     union all
  3150.         select
  3151.             db_name(),
  3152.             user_name(o1.uid),
  3153.             object_name(k.depid),
  3154.             c2.name,
  3155.             db_name(),
  3156.             user_name(o2.uid),
  3157.             object_name(k.id),
  3158.             c1.name,
  3159.             8
  3160.         from
  3161.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3162.         where
  3163.             c1.id = k.id
  3164.             and k.type = 2    /* Foreign type key */
  3165.             and c1.colid = k.key8
  3166.             and c2.id = k.depid
  3167.             and c2.colid = k.depkey8
  3168.             and o1.id = k.depid
  3169.             and o2.id = k.id
  3170.  
  3171.     if @order_by_pk = 1 /*    If order by PK fields */
  3172.         select
  3173.             PKTABLE_QUALIFIER,
  3174.             PKTABLE_OWNER,
  3175.             PKTABLE_NAME,
  3176.             PKCOLUMN_NAME,
  3177.             FKTABLE_QUALIFIER,
  3178.             FKTABLE_OWNER,
  3179.             FKTABLE_NAME,
  3180.             FKCOLUMN_NAME,
  3181.             KEY_SEQ,
  3182.             UPDATE_RULE = convert(smallint, null),
  3183.             DELETE_RULE = convert(smallint,null),
  3184.             FK_NAME = convert(varchar(32),null),
  3185.             PK_NAME = convert(varchar(32),null),
  3186.             DEFERRABILITY = convert(smallint,7)
  3187.         from #fkeys
  3188.         where FKTABLE_NAME like @fktable_name
  3189.             and FKTABLE_OWNER like @fktable_owner
  3190.             and PKTABLE_NAME  like @pktable_name
  3191.             and PKTABLE_OWNER like @pktable_owner
  3192.         order by 1, 2, 3, 9
  3193.     else        /*    Order by FK fields */
  3194.         select
  3195.             PKTABLE_QUALIFIER,
  3196.             PKTABLE_OWNER,
  3197.             PKTABLE_NAME,
  3198.             PKCOLUMN_NAME,
  3199.             FKTABLE_QUALIFIER,
  3200.             FKTABLE_OWNER,
  3201.             FKTABLE_NAME,
  3202.             FKCOLUMN_NAME,
  3203.             KEY_SEQ,
  3204.             UPDATE_RULE = convert(smallint,null),
  3205.             DELETE_RULE = convert(smallint,null),
  3206.             FK_NAME = convert(varchar(32),null),
  3207.             PK_NAME = convert(varchar(32),null),
  3208.             DEFERRABILITY = convert(smallint,7)
  3209.         from #fkeys
  3210.         where FKTABLE_NAME like @fktable_name
  3211.             and FKTABLE_OWNER like @fktable_owner
  3212.             and PKTABLE_NAME  like @pktable_name
  3213.             and PKTABLE_OWNER like @pktable_owner
  3214.         order by 5, 6, 7, 9
  3215. go
  3216.  
  3217. if (charindex('6.00', @@version) = 0 and
  3218.     charindex('6.50', @@version) = 0 and
  3219.     charindex('7.00', @@version) = 0)
  3220. begin
  3221.     print ''
  3222.     print ''
  3223.     print 'Warning:'
  3224.     print 'you are installing the stored procedures '
  3225.     print 'on a pre 6.0 SQL Server.'
  3226.     print 'Ignore the following errors.'
  3227. end
  3228. else
  3229.     drop proc sp_fkeys
  3230. go
  3231.  
  3232. /*    Procedure for 6.0 and 6.50 servers */
  3233. CREATE PROCEDURE sp_fkeys(
  3234.                @pktable_name        varchar(32) = null,
  3235.                @pktable_owner        varchar(32) = null,
  3236.                @pktable_qualifier    varchar(32) = null,
  3237.                @fktable_name        varchar(32) = null,
  3238.                @fktable_owner        varchar(32) = null,
  3239.                @fktable_qualifier    varchar(32) = null )
  3240. as
  3241.     set nocount on
  3242.     DECLARE @pktable_id            int
  3243.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  3244.     DECLARE @fktable_id            int
  3245.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  3246.     declare    @order_by_pk        int
  3247.  
  3248.     select  @order_by_pk = 0
  3249.  
  3250.     if (@pktable_name is null) and (@fktable_name is null)
  3251.     begin    /* If neither primary key nor foreign key table names given */
  3252.         raiserror (15252,-1,-1)
  3253.         return
  3254.     end
  3255.     if @fktable_qualifier is not null
  3256.     begin
  3257.         if db_name() <> @fktable_qualifier
  3258.         begin    /* If qualifier doesn't match current database */
  3259.             raiserror (15250, -1,-1)
  3260.             return
  3261.         end
  3262.     end
  3263.     if @pktable_qualifier is not null
  3264.     begin
  3265.         if db_name() <> @pktable_qualifier
  3266.         begin    /* If qualifier doesn't match current database */
  3267.             raiserror (15250, -1,-1)
  3268.             return
  3269.         end
  3270.     end
  3271.  
  3272.     if @pktable_owner is null
  3273.     begin    /* If unqualified primary key table name */
  3274.         SELECT @pkfull_table_name = @pktable_name
  3275.     end
  3276.     else
  3277.     begin    /* Qualified primary key table name */
  3278.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3279.     end
  3280.     /*    Get Object ID */
  3281.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3282.  
  3283.     if @fktable_owner is null
  3284.     begin    /* If unqualified foreign key table name */
  3285.         SELECT @fkfull_table_name = @fktable_name
  3286.     end
  3287.     else
  3288.     begin    /* Qualified foreign key table name */
  3289.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3290.     end
  3291.     /*    Get Object ID */
  3292.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3293.  
  3294.     if @fktable_name is not null
  3295.     begin
  3296.         if @fktable_id is null
  3297.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3298.     end
  3299.  
  3300.     if @pktable_name is null
  3301.     begin /*  If table name not supplied, match all */
  3302.         select @order_by_pk = 1
  3303.     end
  3304.     else
  3305.     begin
  3306.         if @pktable_id is null
  3307.         begin
  3308.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3309.         end
  3310.     end
  3311.  
  3312.     if (@@trancount <> 0 and
  3313.         charindex('6.50', @@version) = 0)
  3314.     begin    /* If inside a transaction */
  3315.         raiserror (15002,-1,-1,'sp_fkeys')
  3316.         return
  3317.     end
  3318.  
  3319.     create table #fkeys(
  3320.              pkdb_id        int NOT NULL,
  3321.              pktable_id     int NOT NULL,
  3322.              pkcolid        int NOT NULL,
  3323.              fkdb_id        int NOT NULL,
  3324.              fktable_id        int NOT NULL,
  3325.              fkcolid        int NOT NULL,
  3326.              KEY_SEQ        smallint NOT NULL,
  3327.              fk_id            int NOT NULL,
  3328.              pk_id            int NOT NULL)
  3329.  
  3330.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3331.     /*    Process syskeys for each relationship */
  3332.     /*    The inserts below adds a row to the temp table for each of the
  3333.         16 possible relationships */
  3334.     insert into #fkeys
  3335.         select
  3336.             r.rkeydbid,
  3337.             r.rkeyid,
  3338.             r.rkey1,
  3339.             r.fkeydbid,
  3340.             r.fkeyid,
  3341.             r.fkey1,
  3342.             1,
  3343.             r.constid,
  3344.             s.constid
  3345.         from
  3346.             sysreferences r, sysconstraints s
  3347.         where    r.rkeyid = s.id
  3348.             AND (s.status & 0xf) = 1
  3349.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3350.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3351.       union all
  3352.         select
  3353.             r.rkeydbid,
  3354.             r.rkeyid,
  3355.             r.rkey2,
  3356.             r.fkeydbid,
  3357.             r.fkeyid,
  3358.             r.fkey2,
  3359.             2,
  3360.             r.constid,
  3361.             s.constid
  3362.         from
  3363.             sysreferences r, sysconstraints s
  3364.         where    r.rkeyid = s.id
  3365.             AND (s.status & 0xf) = 1
  3366.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3367.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3368.       union all
  3369.         select
  3370.             r.rkeydbid,
  3371.             r.rkeyid,
  3372.             r.rkey3,
  3373.             r.fkeydbid,
  3374.             r.fkeyid,
  3375.             r.fkey3,
  3376.             3,
  3377.             r.constid,
  3378.             s.constid
  3379.         from
  3380.             sysreferences r, sysconstraints s
  3381.         where    r.rkeyid = s.id
  3382.             AND (s.status & 0xf) = 1
  3383.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3384.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3385.       union all
  3386.         select
  3387.             r.rkeydbid,
  3388.             r.rkeyid,
  3389.             r.rkey4,
  3390.             r.fkeydbid,
  3391.             r.fkeyid,
  3392.             r.fkey4,
  3393.             4,
  3394.             r.constid,
  3395.             s.constid
  3396.         from
  3397.             sysreferences r, sysconstraints s
  3398.         where    r.rkeyid = s.id
  3399.             AND (s.status & 0xf) = 1
  3400.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3401.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3402.       union all
  3403.         select
  3404.             r.rkeydbid,
  3405.             r.rkeyid,
  3406.             r.rkey5,
  3407.             r.fkeydbid,
  3408.             r.fkeyid,
  3409.             r.fkey5,
  3410.             5,
  3411.             r.constid,
  3412.             s.constid
  3413.         from
  3414.             sysreferences r, sysconstraints s
  3415.         where    r.rkeyid = s.id
  3416.             AND (s.status & 0xf) = 1
  3417.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3418.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3419.       union all
  3420.         select
  3421.             r.rkeydbid,
  3422.             r.rkeyid,
  3423.             r.rkey6,
  3424.             r.fkeydbid,
  3425.             r.fkeyid,
  3426.             r.fkey6,
  3427.             6,
  3428.             r.constid,
  3429.             s.constid
  3430.         from
  3431.             sysreferences r, sysconstraints s
  3432.         where    r.rkeyid = s.id
  3433.             AND (s.status & 0xf) = 1
  3434.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3435.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3436.       union all
  3437.         select
  3438.             r.rkeydbid,
  3439.             r.rkeyid,
  3440.             r.rkey7,
  3441.             r.fkeydbid,
  3442.             r.fkeyid,
  3443.             r.fkey7,
  3444.             7,
  3445.             r.constid,
  3446.             s.constid
  3447.         from
  3448.             sysreferences r, sysconstraints s
  3449.         where    r.rkeyid = s.id
  3450.             AND (s.status & 0xf) = 1
  3451.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3452.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3453.       union all
  3454.         select
  3455.             r.rkeydbid,
  3456.             r.rkeyid,
  3457.             r.rkey8,
  3458.             r.fkeydbid,
  3459.             r.fkeyid,
  3460.             r.fkey8,
  3461.             8,
  3462.             r.constid,
  3463.             s.constid
  3464.         from
  3465.             sysreferences r, sysconstraints s
  3466.         where    r.rkeyid = s.id
  3467.             AND (s.status & 0xf) = 1
  3468.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3469.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3470.       union all
  3471.         select
  3472.             r.rkeydbid,
  3473.             r.rkeyid,
  3474.             r.rkey9,
  3475.             r.fkeydbid,
  3476.             r.fkeyid,
  3477.             r.fkey9,
  3478.             9,
  3479.             r.constid,
  3480.             s.constid
  3481.         from
  3482.             sysreferences r, sysconstraints s
  3483.         where    r.rkeyid = s.id
  3484.             AND (s.status & 0xf) = 1
  3485.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3486.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3487.       union all
  3488.         select
  3489.             r.rkeydbid,
  3490.             r.rkeyid,
  3491.             r.rkey10,
  3492.             r.fkeydbid,
  3493.             r.fkeyid,
  3494.             r.fkey10,
  3495.             10,
  3496.             r.constid,
  3497.             s.constid
  3498.         from
  3499.             sysreferences r, sysconstraints s
  3500.         where    r.rkeyid = s.id
  3501.             AND (s.status & 0xf) = 1
  3502.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3503.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3504.       union all
  3505.         select
  3506.             r.rkeydbid,
  3507.             r.rkeyid,
  3508.             r.rkey11,
  3509.             r.fkeydbid,
  3510.             r.fkeyid,
  3511.             r.fkey11,
  3512.             11,
  3513.             r.constid,
  3514.             s.constid
  3515.         from
  3516.             sysreferences r, sysconstraints s
  3517.         where    r.rkeyid = s.id
  3518.             AND (s.status & 0xf) = 1
  3519.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3520.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3521.       union all
  3522.         select
  3523.             r.rkeydbid,
  3524.             r.rkeyid,
  3525.             r.rkey12,
  3526.             r.fkeydbid,
  3527.             r.fkeyid,
  3528.             r.fkey12,
  3529.             12,
  3530.             r.constid,
  3531.             s.constid
  3532.         from
  3533.             sysreferences r, sysconstraints s
  3534.         where    r.rkeyid = s.id
  3535.             AND (s.status & 0xf) = 1
  3536.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3537.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3538.       union all
  3539.         select
  3540.             r.rkeydbid,
  3541.             r.rkeyid,
  3542.             r.rkey13,
  3543.             r.fkeydbid,
  3544.             r.fkeyid,
  3545.             r.fkey13,
  3546.             13,
  3547.             r.constid,
  3548.             s.constid
  3549.         from
  3550.             sysreferences r, sysconstraints s
  3551.         where    r.rkeyid = s.id
  3552.             AND (s.status & 0xf) = 1
  3553.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3554.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3555.       union all
  3556.         select
  3557.             r.rkeydbid,
  3558.             r.rkeyid,
  3559.             r.rkey14,
  3560.             r.fkeydbid,
  3561.             r.fkeyid,
  3562.             r.fkey14,
  3563.             14,
  3564.             r.constid,
  3565.             s.constid
  3566.         from
  3567.             sysreferences r, sysconstraints s
  3568.         where    r.rkeyid = s.id
  3569.             AND (s.status & 0xf) = 1
  3570.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3571.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3572.       union all
  3573.         select
  3574.             r.rkeydbid,
  3575.             r.rkeyid,
  3576.             r.rkey15,
  3577.             r.fkeydbid,
  3578.             r.fkeyid,
  3579.             r.fkey15,
  3580.             15,
  3581.             r.constid,
  3582.             s.constid
  3583.         from
  3584.             sysreferences r, sysconstraints s
  3585.         where    r.rkeyid = s.id
  3586.             AND (s.status & 0xf) = 1
  3587.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3588.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3589.       union all
  3590.         select
  3591.             r.rkeydbid,
  3592.             r.rkeyid,
  3593.             r.rkey16,
  3594.             r.fkeydbid,
  3595.             r.fkeyid,
  3596.             r.fkey16,
  3597.             16,
  3598.             r.constid,
  3599.             s.constid
  3600.         from
  3601.             sysreferences r, sysconstraints s
  3602.         where    r.rkeyid = s.id
  3603.             AND (s.status & 0xf) = 1
  3604.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3605.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3606.  
  3607.     if @order_by_pk = 1 /*    If order by PK fields */
  3608.         select
  3609.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3610.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3611.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3612.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3613.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3614.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3615.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3616.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3617.             KEY_SEQ,
  3618.             UPDATE_RULE = convert(smallint,1),
  3619.             DELETE_RULE = convert(smallint,1),
  3620.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3621.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3622.             DEFERRABILITY = convert(smallint,7)
  3623.         from #fkeys f,
  3624.             sysobjects o1, sysobjects o2,
  3625.             syscolumns c1, syscolumns c2
  3626.         where    o1.id = f.pktable_id
  3627.             AND o2.id = f.fktable_id
  3628.             AND c1.id = f.pktable_id
  3629.             AND c2.id = f.fktable_id
  3630.             AND c1.colid = f.pkcolid
  3631.             AND c2.colid = f.fkcolid
  3632.         order by 1,2,3,9
  3633.     else        /*    Order by FK fields */
  3634.         select
  3635.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3636.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3637.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3638.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3639.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3640.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3641.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3642.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3643.             KEY_SEQ,
  3644.             UPDATE_RULE = convert(smallint,1),
  3645.             DELETE_RULE = convert(smallint,1),
  3646.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3647.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3648.             DEFERRABILITY = convert(smallint,7)
  3649.         from #fkeys f,
  3650.             sysobjects o1, sysobjects o2,
  3651.             syscolumns c1, syscolumns c2
  3652.         where    o1.id = f.pktable_id
  3653.             AND o2.id = f.fktable_id
  3654.             AND c1.id = f.pktable_id
  3655.             AND c2.id = f.fktable_id
  3656.             AND c1.colid = f.pkcolid
  3657.             AND c2.colid = f.fkcolid
  3658.         order by 5,6,7,9
  3659. go
  3660.  
  3661. if (charindex('7.00', @@version) = 0)
  3662. begin
  3663.     print ''
  3664.     print ''
  3665.     print 'Warning:'
  3666.     print 'you are installing the stored procedures '
  3667.     print 'on a pre 7.0 SQL Server.'
  3668.     print 'Ignore the following errors.'
  3669. end
  3670. else
  3671.     drop proc sp_fkeys
  3672. go
  3673.  
  3674. /*    Procedure for 7.0 and later servers */
  3675. CREATE PROCEDURE sp_fkeys(
  3676.                @pktable_name        sysname = null,
  3677.                @pktable_owner        sysname = null,
  3678.                @pktable_qualifier    sysname = null,
  3679.                @fktable_name        sysname = null,
  3680.                @fktable_owner        sysname = null,
  3681.                @fktable_qualifier    sysname = null )
  3682. as
  3683.     set nocount on
  3684.     DECLARE @pktable_id            int
  3685.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3686.     DECLARE @fktable_id            int
  3687.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3688.     declare    @order_by_pk        int
  3689.  
  3690.     /* select 'XXX starting table creation' */
  3691.  
  3692.     create table #fkeysall(
  3693.             rkeyid int NOT NULL,
  3694.             rkey1 int NOT NULL,
  3695.                 rkey2 int NOT NULL,
  3696.                 rkey3 int NOT NULL,
  3697.                 rkey4 int NOT NULL,
  3698.                 rkey5 int NOT NULL,
  3699.                 rkey6 int NOT NULL,
  3700.                 rkey7 int NOT NULL,
  3701.                 rkey8 int NOT NULL,
  3702.                 rkey9 int NOT NULL,
  3703.                 rkey10 int NOT NULL,
  3704.                 rkey11 int NOT NULL,
  3705.                 rkey12 int NOT NULL,
  3706.                 rkey13 int NOT NULL,
  3707.                 rkey14 int NOT NULL,
  3708.                 rkey15 int NOT NULL,
  3709.                 rkey16 int NOT NULL,
  3710.             fkeyid int NOT NULL,
  3711.             fkey1 int NOT NULL,
  3712.                 fkey2 int NOT NULL,
  3713.                 fkey3 int NOT NULL,
  3714.                 fkey4 int NOT NULL,
  3715.                 fkey5 int NOT NULL,
  3716.                 fkey6 int NOT NULL,
  3717.                 fkey7 int NOT NULL,
  3718.                 fkey8 int NOT NULL,
  3719.                 fkey9 int NOT NULL,
  3720.                 fkey10 int NOT NULL,
  3721.                 fkey11 int NOT NULL,
  3722.                 fkey12 int NOT NULL,
  3723.                 fkey13 int NOT NULL,
  3724.                 fkey14 int NOT NULL,
  3725.                 fkey15 int NOT NULL,
  3726.                 fkey16 int NOT NULL,
  3727.             constid int NOT NULL,
  3728.             name sysname NOT NULL)
  3729.  
  3730.     create table #fkeys(
  3731.             pktable_id        int NOT NULL,
  3732.             pkcolid         int NOT NULL,
  3733.             fktable_id        int NOT NULL,
  3734.             fkcolid         int NOT NULL,
  3735.             KEY_SEQ         smallint NOT NULL,
  3736.             fk_id            int NOT NULL,
  3737.             PK_NAME            sysname NOT NULL)
  3738.  
  3739.     create table #fkeysout(
  3740.             PKTABLE_QUALIFIER sysname NULL,
  3741.             PKTABLE_OWNER sysname NULL,
  3742.             PKTABLE_NAME sysname NOT NULL,
  3743.             PKCOLUMN_NAME sysname NOT NULL,
  3744.             FKTABLE_QUALIFIER sysname NULL,
  3745.             FKTABLE_OWNER sysname NULL,
  3746.             FKTABLE_NAME sysname NOT NULL,
  3747.             FKCOLUMN_NAME sysname NOT NULL,
  3748.             KEY_SEQ smallint NOT NULL,
  3749.             UPDATE_RULE smallint NULL,
  3750.             DELETE_RULE smallint NULL,
  3751.             FK_NAME sysname NULL,
  3752.             PK_NAME sysname NULL,
  3753.             DEFERRABILITY smallint null)
  3754.  
  3755.     /* select 'XXX starting parameter analysis' */
  3756.  
  3757.     select  @order_by_pk = 0
  3758.  
  3759.     if (@pktable_name is null) and (@fktable_name is null)
  3760.     begin    /* If neither primary key nor foreign key table names given */
  3761.         raiserror (15252,-1,-1)
  3762.         return
  3763.     end
  3764.     if @fktable_qualifier is not null
  3765.     begin
  3766.         if db_name() <> @fktable_qualifier
  3767.         begin    /* If qualifier doesn't match current database */
  3768.             raiserror (15250, -1,-1)
  3769.             return
  3770.         end
  3771.     end
  3772.     if @pktable_qualifier is not null
  3773.     begin
  3774.         if db_name() <> @pktable_qualifier
  3775.         begin    /* If qualifier doesn't match current database */
  3776.             raiserror (15250, -1,-1)
  3777.             return
  3778.         end
  3779.     end
  3780.  
  3781.     if @pktable_owner is null
  3782.     begin    /* If unqualified primary key table name */
  3783.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3784.     end
  3785.     else
  3786.     begin    /* Qualified primary key table name */
  3787.         if @pktable_owner = ''
  3788.         begin    /* If empty owner name */
  3789.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3790.         end
  3791.         else
  3792.         begin
  3793.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3794.                 '.' + quotename(@pktable_name)
  3795.         end
  3796.     end
  3797.     /*    Get Object ID */
  3798.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3799.  
  3800.     if @fktable_owner is null
  3801.     begin    /* If unqualified foreign key table name */
  3802.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3803.     end
  3804.     else
  3805.     begin    /* Qualified foreign key table name */
  3806.         if @fktable_owner = ''
  3807.         begin    /* If empty owner name */
  3808.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3809.         end
  3810.         else
  3811.         begin
  3812.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3813.                 '.' + quotename(@fktable_name)
  3814.         end
  3815.     end
  3816.     /*    Get Object ID */
  3817.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3818.  
  3819.     if @fktable_name is not null
  3820.     begin
  3821.         if @fktable_id is null
  3822.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3823.     end
  3824.  
  3825.     if @pktable_name is null
  3826.     begin /*  If table name not supplied, match all */
  3827.         select @order_by_pk = 1
  3828.     end
  3829.     else
  3830.     begin
  3831.         if @pktable_id is null
  3832.         begin
  3833.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3834.         end
  3835.     end
  3836.  
  3837.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3838.     /*    Process syskeys for each relationship */
  3839.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3840.         them out with a 16-way "insert select ... union select ..." */
  3841.  
  3842.     /* select 'XXX starting data analysis' */
  3843.  
  3844.     insert into #fkeysall
  3845.         select
  3846.             r.rkeyid,
  3847.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3848.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3849.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3850.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3851.             r.fkeyid,
  3852.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3853.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3854.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3855.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3856.             r.constid,
  3857.             i.name
  3858.         from
  3859.             sysreferences r, sysobjects o, sysindexes i
  3860.         where    r.constid = o.id
  3861.             AND o.xtype = 'F'
  3862.             AND r.rkeyindid = i.indid
  3863.             AND r.rkeyid = i.id
  3864.             AND r.rkeyid between isnull(@pktable_id, 0)
  3865.                             and isnull(@pktable_id, 0x7fffffff)
  3866.             AND r.fkeyid between isnull(@fktable_id, 0)
  3867.                             and isnull(@fktable_id, 0x7fffffff)
  3868.  
  3869.     /* select count (*) as 'XXX countall' from #fkeysall */
  3870.  
  3871.     insert into #fkeys
  3872.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3873.             from #fkeysall
  3874.         union all
  3875.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3876.             from #fkeysall
  3877.         union all
  3878.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3879.             from #fkeysall
  3880.         union all
  3881.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3882.             from #fkeysall
  3883.         union all
  3884.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3885.             from #fkeysall
  3886.         union all
  3887.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3888.             from #fkeysall
  3889.         union all
  3890.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3891.             from #fkeysall
  3892.         union all
  3893.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3894.             from #fkeysall
  3895.         union all
  3896.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3897.             from #fkeysall
  3898.         union all
  3899.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3900.             from #fkeysall
  3901.         union all
  3902.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3903.             from #fkeysall
  3904.         union all
  3905.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3906.             from #fkeysall
  3907.         union all
  3908.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3909.             from #fkeysall
  3910.         union all
  3911.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3912.             from #fkeysall
  3913.         union all
  3914.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3915.             from #fkeysall
  3916.         union all
  3917.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3918.             from #fkeysall
  3919.  
  3920.     /* select count (*) as 'XXX count' from #fkeys */
  3921.  
  3922.     insert into #fkeysout
  3923.         select
  3924.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3925.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3926.             PKTABLE_NAME = convert(sysname,o1.name),
  3927.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3928.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3929.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3930.             FKTABLE_NAME = convert(sysname,o2.name),
  3931.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3932.             KEY_SEQ,
  3933.             UPDATE_RULE = convert(smallint,1),
  3934.             DELETE_RULE = convert(smallint,1),
  3935.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3936.             PK_NAME,
  3937.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3938.         from #fkeys f,
  3939.             sysobjects o1, sysobjects o2,
  3940.             syscolumns c1, syscolumns c2
  3941.         where    o1.id = f.pktable_id
  3942.             AND o2.id = f.fktable_id
  3943.             AND c1.id = f.pktable_id
  3944.             AND c2.id = f.fktable_id
  3945.             AND c1.colid = f.pkcolid
  3946.             AND c2.colid = f.fkcolid
  3947.  
  3948.     /* select count (*) as 'XXX countout' from #fkeysout */
  3949.  
  3950.     if @order_by_pk = 1 /*    If order by PK fields */
  3951.         select
  3952.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3953.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3954.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3955.         from #fkeysout
  3956.         order by 1,2,3,9
  3957.     else        /*    Order by FK fields */
  3958.         select
  3959.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3960.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3961.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3962.         from #fkeysout
  3963.         order by 5,6,7,9
  3964. go
  3965.  
  3966. grant execute on sp_fkeys to public
  3967. go
  3968.  
  3969. dump tran master with no_log
  3970. go
  3971.  
  3972. print 'creating sp_pkeys'
  3973. go
  3974.  
  3975. /*    Procedure for pre-6.0 server */
  3976. CREATE PROCEDURE sp_pkeys(
  3977.                @table_name        varchar(32),
  3978.                @table_owner     varchar(32) = null,
  3979.                @table_qualifier varchar(32) = null )
  3980. as
  3981.     set nocount on
  3982.     if @table_qualifier is not null
  3983.     begin
  3984.         if db_name() <> @table_qualifier
  3985.         begin    /* If qualifier doesn't match current database */
  3986.             raiserror 20001 '~~Rush_5~~'
  3987.             return
  3988.         end
  3989.     end
  3990.     if @table_owner is null /*    If owner not supplied, match all */
  3991.         select @table_owner = '%'
  3992.     if @@trancount <> 0
  3993.     begin    /* If inside a transaction */
  3994.         raiserror 20003 '~~Rush_35~~'
  3995.         return
  3996.     end
  3997.  
  3998.     create table #pkeys(
  3999.              TABLE_QUALIFIER varchar(32) NULL,
  4000.              TABLE_OWNER     varchar(32) NULL,
  4001.              TABLE_NAME      varchar(32) NOT NULL,
  4002.              COLUMN_NAME     varchar(32) NOT NULL,
  4003.              KEY_SEQ         smallint NOT NULL)
  4004.  
  4005.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4006.     /*    Process syskeys for each relationship */
  4007.     /*    The inserts below adds a row to the temp table for each of the
  4008.         8 possible relationships */
  4009.     insert into #pkeys
  4010.         select
  4011.             db_name(),
  4012.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4013.             object_name(k.id),
  4014.             c.name,
  4015.             1
  4016.         from
  4017.             syskeys k, syscolumns c
  4018.         where
  4019.             c.id = k.id
  4020.             and k.type = 1    /* Primary type key */
  4021.             and c.colid = k.key1
  4022.     if (@@rowcount = 0)
  4023.         goto done
  4024.  
  4025.     insert into #pkeys
  4026.         select
  4027.             db_name(),
  4028.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4029.             object_name(k.id),
  4030.             c.name,
  4031.             2
  4032.         from
  4033.             syskeys k, syscolumns c
  4034.         where
  4035.             c.id = k.id
  4036.             and k.type = 1    /* Primary type key */
  4037.             and c.colid = key2
  4038.     if (@@rowcount = 0)
  4039.         goto done
  4040.  
  4041.     insert into #pkeys
  4042.         select
  4043.             db_name(),
  4044.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4045.             object_name(k.id),
  4046.             c.name,
  4047.             3
  4048.         from
  4049.             syskeys k, syscolumns c
  4050.         where
  4051.             c.id = k.id
  4052.             and k.type = 1    /* Primary type key */
  4053.             and c.colid = key3
  4054.     if (@@rowcount = 0)
  4055.         goto done
  4056.  
  4057.     insert into #pkeys
  4058.         select
  4059.             db_name(),
  4060.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4061.             object_name(k.id),
  4062.             c.name,
  4063.             4
  4064.         from
  4065.             syskeys k, syscolumns c
  4066.         where
  4067.             c.id = k.id
  4068.             and k.type = 1    /* Primary type key */
  4069.             and c.colid = key4
  4070.     if (@@rowcount = 0)
  4071.         goto done
  4072.  
  4073.     insert into #pkeys
  4074.         select
  4075.             db_name(),
  4076.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4077.             object_name(k.id),
  4078.             c.name,
  4079.             5
  4080.         from
  4081.             syskeys k, syscolumns c
  4082.         where
  4083.             c.id = k.id
  4084.             and k.type = 1    /* Primary type key */
  4085.             and c.colid = key5
  4086.     if (@@rowcount = 0)
  4087.         goto done
  4088.  
  4089.     insert into #pkeys
  4090.         select
  4091.             db_name(),
  4092.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4093.             object_name(k.id),
  4094.             c.name,
  4095.             6
  4096.         from
  4097.             syskeys k, syscolumns c
  4098.         where
  4099.             c.id = k.id
  4100.             and k.type = 1    /* Primary type key */
  4101.             and c.colid = key6
  4102.     if (@@rowcount = 0)
  4103.         goto done
  4104.  
  4105.     insert into #pkeys
  4106.         select
  4107.             db_name(),
  4108.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4109.             object_name(k.id),
  4110.             c.name,
  4111.             7
  4112.         from
  4113.             syskeys k, syscolumns c
  4114.         where
  4115.             c.id = k.id
  4116.             and k.type = 1    /* Primary type key */
  4117.             and c.colid = key7
  4118.     if (@@rowcount = 0)
  4119.         goto done
  4120.  
  4121.     insert into #pkeys
  4122.          select
  4123.              db_name(),
  4124.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4125.              object_name(k.id),
  4126.              c.name,
  4127.              8
  4128.          from
  4129.              syskeys k, syscolumns c
  4130.          where
  4131.              c.id = k.id
  4132.              and k.type = 1 /* Primary type key */
  4133.              and c.colid = key8
  4134.  
  4135.     done:
  4136.     select
  4137.         TABLE_QUALIFIER,
  4138.         TABLE_OWNER,
  4139.         TABLE_NAME,
  4140.         COLUMN_NAME,
  4141.         KEY_SEQ,
  4142.         PK_NAME = convert(varchar(32),null)
  4143.     from #pkeys
  4144.     where TABLE_NAME = @table_name
  4145.         and TABLE_OWNER like @table_owner
  4146.     order by 1, 2, 3, 5
  4147. go
  4148.  
  4149. if (charindex('6.00', @@version) = 0 and
  4150.     charindex('6.50', @@version) = 0 and
  4151.     charindex('7.00', @@version) = 0)
  4152. begin
  4153.     print ''
  4154.     print ''
  4155.     print 'Warning:'
  4156.     print 'you are installing the stored procedures '
  4157.     print 'on a pre 6.0 SQL Server.'
  4158.     print 'Ignore the following error.'
  4159. end
  4160. else
  4161.     drop proc sp_pkeys
  4162. go
  4163.  
  4164. /*    Procedure for 6.0 and 6.50 servers */
  4165. CREATE PROCEDURE sp_pkeys(
  4166.                @table_name        sysname,
  4167.                @table_owner     sysname = null,
  4168.                @table_qualifier sysname = null )
  4169. as
  4170.     DECLARE @table_id            int
  4171.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4172.  
  4173.     if @table_qualifier is not null
  4174.     begin
  4175.         if db_name() <> @table_qualifier
  4176.         begin    /* If qualifier doesn't match current database */
  4177.             raiserror (15250, -1,-1)
  4178.             return
  4179.         end
  4180.     end
  4181.     if @table_owner is null
  4182.     begin    /* If unqualified table name */
  4183.         SELECT @full_table_name = @table_name
  4184.     end
  4185.     else
  4186.     begin    /* Qualified table name */
  4187.         if @table_owner = ''
  4188.         begin    /* If empty owner name */
  4189.             SELECT @full_table_name = @table_owner
  4190.         end
  4191.         else
  4192.         begin
  4193.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4194.         end
  4195.     end
  4196.     /*    Get Object ID */
  4197.     SELECT @table_id = object_id(@full_table_name)
  4198.  
  4199.     select
  4200.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4201.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4202.         TABLE_NAME = convert(sysname,o.name),
  4203.         COLUMN_NAME = convert(sysname,c.name),
  4204.         KEY_SEQ = convert(smallint,c1.colid),
  4205.         PK_NAME = convert(sysname,i.name)
  4206.     from
  4207.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4208.     where
  4209.         o.id = @table_id
  4210.         and o.id = c.id
  4211.         and o.id = i.id
  4212.         and (i.status & 0x800) = 0x800
  4213.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4214.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4215.         and c1.id = @table_id
  4216.     order by 1, 2, 3, 5
  4217. go
  4218.  
  4219. if (charindex('7.00', @@version) = 0)
  4220. begin
  4221.     print ''
  4222.     print ''
  4223.     print 'Warning:'
  4224.     print 'you are installing the stored procedures '
  4225.     print 'on a pre 7.0 SQL Server.'
  4226.     print 'Ignore the following errors.'
  4227. end
  4228. else
  4229.     drop proc sp_pkeys
  4230. go
  4231.  
  4232. /*    Procedure for 7.0 and later servers */
  4233. CREATE PROCEDURE sp_pkeys(
  4234.                @table_name        sysname,
  4235.                @table_owner     sysname = null,
  4236.                @table_qualifier sysname = null )
  4237. as
  4238.     DECLARE @table_id            int
  4239.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4240.  
  4241.     if @table_qualifier is not null
  4242.     begin
  4243.         if db_name() <> @table_qualifier
  4244.         begin    /* If qualifier doesn't match current database */
  4245.             raiserror (15250, -1,-1)
  4246.             return
  4247.         end
  4248.     end
  4249.     if @table_owner is null
  4250.     begin    /* If unqualified table name */
  4251.         SELECT @full_table_name = quotename(@table_name)
  4252.     end
  4253.     else
  4254.     begin    /* Qualified table name */
  4255.         if @table_owner = ''
  4256.         begin    /* If empty owner name */
  4257.             SELECT @full_table_name = quotename(@table_owner)
  4258.         end
  4259.         else
  4260.         begin
  4261.             SELECT @full_table_name = quotename(@table_owner) +
  4262.                 '.' + quotename(@table_name)
  4263.         end
  4264.     end
  4265.     /*    Get Object ID */
  4266.     SELECT @table_id = object_id(@full_table_name)
  4267.  
  4268.     select
  4269.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4270.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4271.         TABLE_NAME = convert(sysname,o.name),
  4272.         COLUMN_NAME = convert(sysname,c.name),
  4273.         KEY_SEQ = convert(smallint,c1.colid),
  4274.         PK_NAME = convert(sysname,i.name)
  4275.     from
  4276.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4277.     where
  4278.         o.id = @table_id
  4279.         and o.id = c.id
  4280.         and o.id = i.id
  4281.         and (i.status & 0x800) = 0x800
  4282.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4283.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4284.         and c1.id = @table_id
  4285.     order by 1, 2, 3, 5
  4286. go
  4287.  
  4288. grant execute on sp_pkeys to public
  4289. go
  4290.  
  4291. dump tran master with no_log
  4292. go
  4293.  
  4294. print 'creating sp_server_info'
  4295. go
  4296.  
  4297. create proc sp_server_info (
  4298.             @attribute_id  int = null)
  4299. as
  4300.     if @attribute_id is not null
  4301.         select *
  4302.         from master.dbo.spt_server_info
  4303.         where attribute_id = @attribute_id
  4304.     else
  4305.         select *
  4306.         from master.dbo.spt_server_info
  4307.         order by attribute_id
  4308. go
  4309.  
  4310. grant execute on sp_server_info to public
  4311. go
  4312.  
  4313. dump tran master with no_log
  4314. go
  4315.  
  4316. print 'creating sp_special_columns'
  4317. go
  4318.  
  4319. /*    Procedure for pre-6.0 server */
  4320. CREATE PROCEDURE sp_special_columns (
  4321.                  @table_name        varchar(32),
  4322.                  @table_owner        varchar(32) = null,
  4323.                  @table_qualifier    varchar(32) = null,
  4324.                  @col_type            char(1) = 'R',
  4325.                  @scope                char(1) = 'T',
  4326.                  @nullable            char(1) = 'U',
  4327.                  @ODBCVer            int = 2)
  4328. AS
  4329.     DECLARE @indid                int
  4330.     DECLARE @table_id            int
  4331.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4332.     DECLARE @scopeout            smallint
  4333.  
  4334.     if @col_type not in ('R','V')
  4335.     begin
  4336.         raiserror 20002 '~~Rush_42~~'
  4337.         return
  4338.     end
  4339.  
  4340.     if @scope = 'C'
  4341.         select @scopeout = 0
  4342.     else if @scope = 'T'
  4343.         select @scopeout = 1
  4344.     else
  4345.     begin
  4346.         raiserror 20002 '~~Rush_43~~'
  4347.         return
  4348.     end
  4349.  
  4350.     if @nullable not in ('U','O')
  4351.     begin
  4352.         raiserror 20002 '~~Rush_44~~'
  4353.         return
  4354.     end
  4355.  
  4356.     if @table_qualifier is not null
  4357.     begin
  4358.         if db_name() <> @table_qualifier
  4359.         begin /* If qualifier doesn't match current database */
  4360.             raiserror 20001 '~~Rush_5~~'
  4361.             return
  4362.         end
  4363.     end
  4364.     if @table_owner is null
  4365.     begin     /* If unqualified table name */
  4366.         SELECT @full_table_name = @table_name
  4367.     end
  4368.     else
  4369.     begin    /* Qualified table name */
  4370.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4371.     end
  4372.     /*    Get Object ID */
  4373.     SELECT @table_id = object_id(@full_table_name)
  4374.  
  4375.     if @col_type = 'V'
  4376.     BEGIN /* if ROWVER, just run that query */
  4377.         SELECT
  4378.             SCOPE = convert(smallint,NULL),
  4379.             COLUMN_NAME = convert(varchar(32),c.name),
  4380.             DATA_TYPE = convert(smallint, -3),
  4381.             TYPE_NAME = t.name,
  4382.             "PRECISION" = convert(int,8),
  4383.             LENGTH = convert(int,8),
  4384.             SCALE = convert(smallint, NULL),
  4385.             PSEUDO_COLUMN = convert(smallint,1)
  4386.         FROM
  4387.             systypes t, syscolumns c
  4388.         WHERE
  4389.             c.id = @table_id
  4390.             AND c.usertype = 80 /*    TIMESTAMP */
  4391.             AND t.usertype = 80 /*    TIMESTAMP */
  4392.         RETURN
  4393.     END
  4394.  
  4395.     /* ROWID, now find the id of the 'best' index for this table */
  4396.  
  4397.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4398.                            nullable columns. */
  4399.  
  4400.             SELECT @indid = MIN(indid)
  4401.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4402.                 WHERE
  4403.                     i.status&2 = 2        /*    If Unique Index */
  4404.                      AND c.id = i.id
  4405.                      AND c2.id = c.id
  4406.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4407.                     AND i.id = @table_id
  4408.                     AND indid > 0        /*    Eliminate Table Row */
  4409.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4410.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4411.  
  4412.     ELSE    /* Include indexes that are partially nullable. */
  4413.  
  4414.         SELECT @indid = MIN(indid)
  4415.             FROM sysindexes i
  4416.             WHERE
  4417.                 status&2 = 2        /*    If Unique Index */
  4418.                 AND id = @table_id
  4419.                 AND indid > 0        /*    Eliminate Table Row */
  4420.  
  4421.     SELECT
  4422.         SCOPE = @scopeout,
  4423.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4424.         d.DATA_TYPE,
  4425.         TYPE_NAME = t.name,
  4426.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4427.         LENGTH = isnull(d.length, convert(int,c.length)),
  4428.         SCALE = d.numeric_scale,
  4429.         PSEUDO_COLUMN = convert(smallint,1)
  4430.     FROM
  4431.         sysindexes x,
  4432.         syscolumns c,
  4433.         master.dbo.spt_datatype_info d,
  4434.         systypes t,
  4435.         syscolumns c2    /* Self-join to generate list of index columns and */
  4436.                         /* to extract datatype names */
  4437.     WHERE
  4438.         x.id = @table_id
  4439.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4440.         AND c.id = x.id
  4441.         AND c2.id = x.id
  4442.         AND c2.colid < keycnt+(x.status&16)/16
  4443.         AND x.indid = @indid
  4444.         AND t.type = d.ss_dtype
  4445.         AND c.length = d.fixlen
  4446.         AND c.usertype = t.usertype
  4447.  
  4448. go
  4449.  
  4450. if (charindex('6.00', @@version) = 0 and
  4451.     charindex('6.50', @@version) = 0 and
  4452.     charindex('7.00', @@version) = 0)
  4453. begin
  4454.     print ''
  4455.     print ''
  4456.     print 'Warning:'
  4457.     print 'you are installing the stored procedures '
  4458.     print 'on a pre 6.0 SQL Server.'
  4459.     print 'Ignore the following errors.'
  4460. end
  4461. else
  4462.     drop proc sp_special_columns
  4463. go
  4464.  
  4465. /*    Procedure for 6.0 and 6.50 servers */
  4466. CREATE PROCEDURE sp_special_columns (
  4467.                  @table_name        varchar(32),
  4468.                  @table_owner        varchar(32) = null,
  4469.                  @table_qualifier    varchar(32) = null,
  4470.                  @col_type            char(1) = 'R',
  4471.                  @scope                char(1) = 'T',
  4472.                  @nullable            char(1) = 'U',
  4473.                  @ODBCVer            int = 2)
  4474. AS
  4475.     DECLARE @indid                int
  4476.     DECLARE @table_id            int
  4477.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4478.     DECLARE @scopeout            smallint
  4479.  
  4480.     if @col_type not in ('R','V')
  4481.     begin
  4482.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4483.         return
  4484.     end
  4485.  
  4486.     if @scope = 'C'
  4487.         select @scopeout = 0
  4488.     else if @scope = 'T'
  4489.         select @scopeout = 1
  4490.     else
  4491.     begin
  4492.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4493.         return
  4494.     end
  4495.  
  4496.     if @nullable not in ('U','O')
  4497.     begin
  4498.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4499.         return
  4500.     end
  4501.  
  4502.     if @table_qualifier is not null
  4503.     begin
  4504.         if db_name() <> @table_qualifier
  4505.         begin /* If qualifier doesn't match current database */
  4506.             raiserror (15250, -1,-1)
  4507.             return
  4508.         end
  4509.     end
  4510.     if @table_owner is null
  4511.     begin     /* If unqualified table name */
  4512.         SELECT @full_table_name = @table_name
  4513.     end
  4514.     else
  4515.     begin     /* Qualified table name */
  4516.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4517.     end
  4518.     /*    Get Object ID */
  4519.     SELECT @table_id = object_id(@full_table_name)
  4520.  
  4521.     if @col_type = 'V'
  4522.     BEGIN /* if ROWVER, just run that query */
  4523.         SELECT
  4524.             SCOPE = convert(smallint,NULL),
  4525.             COLUMN_NAME = convert(varchar(32),c.name),
  4526.             DATA_TYPE = convert(smallint, -2),
  4527.             TYPE_NAME = t.name,
  4528.             "PRECISION" = convert(int,8),
  4529.             LENGTH = convert(int,8),
  4530.             SCALE = convert(smallint, NULL),
  4531.             PSEUDO_COLUMN = convert(smallint,1)
  4532.         FROM
  4533.             systypes t, syscolumns c
  4534.         WHERE
  4535.             c.id = @table_id
  4536.             AND c.usertype = 80 /*    TIMESTAMP */
  4537.             AND t.usertype = 80 /*    TIMESTAMP */
  4538.         RETURN
  4539.     END
  4540.  
  4541.     /* ROWID, now find the id of the 'best' index for this table */
  4542.  
  4543.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4544.                            nullable columns. */
  4545.  
  4546.         SELECT @indid = MIN(indid)
  4547.             FROM sysindexes i,syscolumns c,syscolumns c2
  4548.             WHERE
  4549.                 i.status&2 = 2        /*    If Unique Index */
  4550.                 AND c.id = i.id
  4551.                 AND c2.id = c.id
  4552.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4553.                 AND i.id = @table_id
  4554.                 AND indid > 0        /*    Eliminate Table Row */
  4555.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4556.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4557.  
  4558.     ELSE    /* Include indexes that are partially nullable. */
  4559.  
  4560.         SELECT @indid = MIN(indid)
  4561.             FROM sysindexes i
  4562.             WHERE
  4563.                 status&2 = 2        /*    If Unique Index */
  4564.                 AND id = @table_id
  4565.                 AND indid > 0        /*    Eliminate Table Row */
  4566.  
  4567.     SELECT
  4568.         SCOPE = @scopeout,
  4569.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4570.         d.DATA_TYPE,
  4571.         convert(varchar(32),case
  4572.             when (t.usertype > 100 or t.usertype in (18,80))
  4573.                 then t.name
  4574.             else d.TYPE_NAME
  4575.         end) TYPE_NAME,
  4576.         convert(int,case
  4577.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4578.             else isnull(convert(int,c.prec), 2147483647)
  4579.         end) "PRECISION",
  4580.         convert(int,case
  4581.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4582.                 convert(int,c.prec+2)
  4583.             else
  4584.                 isnull(d.length, c.length)
  4585.         end) LENGTH,
  4586.         SCALE = convert(smallint, c.scale),
  4587.         PSEUDO_COLUMN = convert(smallint,1)
  4588.     FROM
  4589.         sysindexes x,
  4590.         syscolumns c,
  4591.         master.dbo.spt_datatype_info d,
  4592.         systypes t,
  4593.         syscolumns c2    /* Self-join to generate list of index columns and */
  4594.                         /* to extract datatype names */
  4595.     WHERE
  4596.         x.id = @table_id
  4597.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4598.         AND c.id = x.id
  4599.         AND c2.id = x.id
  4600.         AND c2.colid < x.keycnt+(x.status&16)/16
  4601.         AND x.indid = @indid
  4602.         AND t.type = d.ss_dtype
  4603.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4604.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  4605.         AND c.usertype = t.usertype
  4606. go
  4607.  
  4608. if (charindex('7.00', @@version) = 0)
  4609. begin
  4610.     print ''
  4611.     print ''
  4612.     print 'Warning:'
  4613.     print 'you are installing the stored procedures '
  4614.     print 'on a pre 7.0 SQL Server.'
  4615.     print 'Ignore the following errors.'
  4616. end
  4617. else
  4618.     drop proc sp_special_columns
  4619. go
  4620.  
  4621. /*    Procedure for 7.0 and later servers */
  4622. CREATE PROCEDURE sp_special_columns (
  4623.                  @table_name        sysname,
  4624.                  @table_owner        sysname = null,
  4625.                  @table_qualifier    sysname = null,
  4626.                  @col_type            char(1) = 'R',
  4627.                  @scope                char(1) = 'T',
  4628.                  @nullable            char(1) = 'U',
  4629.                  @ODBCVer            int = 2)
  4630. AS
  4631.     DECLARE @indid                int
  4632.     DECLARE @table_id            int
  4633.     DECLARE @full_table_name    nvarchar(257)
  4634.     DECLARE @scopeout            smallint
  4635.  
  4636.     if @col_type not in ('R','V')
  4637.     begin
  4638.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4639.         return
  4640.     end
  4641.  
  4642.     if @scope = 'C'
  4643.         select @scopeout = 0
  4644.     else if @scope = 'T'
  4645.         select @scopeout = 1
  4646.     else
  4647.     begin
  4648.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4649.         return
  4650.     end
  4651.  
  4652.     if @nullable not in ('U','O')
  4653.     begin
  4654.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4655.         return
  4656.     end
  4657.  
  4658.     if @table_qualifier is not null
  4659.     begin
  4660.         if db_name() <> @table_qualifier
  4661.         begin /* If qualifier doesn't match current database */
  4662.             raiserror (15250, -1,-1)
  4663.             return
  4664.         end
  4665.     end
  4666.     if @table_owner is null
  4667.     begin     /* If unqualified table name */
  4668.         SELECT @full_table_name = quotename(@table_name)
  4669.     end
  4670.     else
  4671.     begin    /* Qualified table name */
  4672.         if @table_owner = ''
  4673.         begin    /* If empty owner name */
  4674.             SELECT @full_table_name = quotename(@table_owner)
  4675.         end
  4676.         else
  4677.         begin
  4678.             SELECT @full_table_name = quotename(@table_owner) +
  4679.                 '.' + quotename(@table_name)
  4680.         end
  4681.     end
  4682.     /*    Get Object ID */
  4683.     SELECT @table_id = object_id(@full_table_name)
  4684.  
  4685.     if @col_type = 'V'
  4686.     BEGIN /* if ROWVER, just run that query */
  4687.         SELECT
  4688.             SCOPE = convert(smallint,NULL),
  4689.             COLUMN_NAME = convert(sysname,c.name),
  4690.             DATA_TYPE = convert(smallint, -2),
  4691.             TYPE_NAME = t.name,
  4692.             "PRECISION" = convert(int,8),
  4693.             LENGTH = convert(int,8),
  4694.             SCALE = convert(smallint, NULL),
  4695.             PSEUDO_COLUMN = convert(smallint,1)
  4696.         FROM
  4697.             systypes t, syscolumns c
  4698.         WHERE
  4699.             not (@table_id is null)
  4700.             AND c.id = @table_id
  4701.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  4702.             AND t.xtype = c.xtype        
  4703.             AND t.xusertype = c.xusertype    
  4704.         RETURN
  4705.     END
  4706.  
  4707.     /* ROWID, now find the id of the 'best' index for this table */
  4708.  
  4709.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4710.                            nullable columns. */
  4711.  
  4712.         SELECT @indid = MIN(indid)
  4713.             FROM sysindexes x, syscolumns c, syscolumns c2
  4714.             WHERE
  4715.                 not (@table_id is null)
  4716.                 AND x.status&2 = 2        /*    If Unique Index */
  4717.                 AND c.id = x.id
  4718.                 AND c2.id = c.id
  4719.                 AND c2.colid < x.keycnt + (x.status&16)/16
  4720.                 AND x.id = @table_id
  4721.                 AND indid > 0        /*    Eliminate Table Row */
  4722.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  4723.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4724.  
  4725.     ELSE    /* Include indexes that are partially nullable. */
  4726.  
  4727.         SELECT @indid = MIN(indid)
  4728.             FROM sysindexes x
  4729.             WHERE
  4730.                 not (@table_id is null)
  4731.                 AND status&2 = 2        /*    If Unique Index */
  4732.                 AND id = @table_id
  4733.                 AND indid > 0        /*    Eliminate Table Row */
  4734.  
  4735.     SELECT
  4736.         SCOPE = @scopeout,
  4737.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  4738.         d.DATA_TYPE,
  4739.         convert(sysname,case
  4740.             when t.xusertype > 255 then t.name
  4741.             else d.TYPE_NAME
  4742.         end) TYPE_NAME,
  4743.         convert(int,case
  4744.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4745.             else OdbcPrec(c.xtype,c.length,c.xprec)
  4746.         end) "PRECISION",
  4747.         convert(int,case
  4748.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4749.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  4750.             else isnull(d.length, c.length)
  4751.         end) LENGTH,
  4752.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4753.         PSEUDO_COLUMN = convert(smallint,1)
  4754.     FROM
  4755.         sysindexes x,
  4756.         syscolumns c,
  4757.         master.dbo.spt_datatype_info d,
  4758.         systypes t,
  4759.         syscolumns c2    /* Self-join to generate list of index columns and */
  4760.                         /* to extract datatype names */
  4761.     WHERE
  4762.         not (@table_id is null)
  4763.         AND x.id = @table_id
  4764.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4765.         AND c.id = x.id
  4766.         AND c2.id = x.id
  4767.         AND c2.colid < x.keycnt + (x.status&16)/16
  4768.         AND x.indid = @indid
  4769.         AND t.xtype = d.ss_dtype
  4770.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4771.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4772.         AND c.xusertype = t.xusertype
  4773. go
  4774.  
  4775. grant execute on sp_special_columns to public
  4776. go
  4777.  
  4778. dump tran master with no_log
  4779. go
  4780.  
  4781. print 'creating sp_sproc_columns'
  4782. go
  4783.  
  4784. /*    Procedure for pre-6.0 server */
  4785. CREATE PROCEDURE sp_sproc_columns (
  4786.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4787.                  @procedure_owner        varchar(96) = null,
  4788.                  @procedure_qualifier    varchar(32) = null,
  4789.                  @column_name            varchar(96) = null,
  4790.                  @ODBCVer                int = 2)
  4791. AS
  4792.     DECLARE @group_num_lower smallint
  4793.     DECLARE @group_num_upper smallint
  4794.     DECLARE @semi_position int
  4795.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4796.     DECLARE @procedure_id int
  4797.  
  4798.     if @column_name is null /*    If column name not supplied, match all */
  4799.         select @column_name = '%'
  4800.     if @procedure_qualifier is not null
  4801.     begin
  4802.         if db_name() <> @procedure_qualifier
  4803.         begin
  4804.             if @procedure_qualifier = ''
  4805.             begin
  4806.                 /* in this case, we need to return an empty result set */
  4807.                 /* because the user has requested a database with an empty name */
  4808.                 select @procedure_name = ''
  4809.                 select @procedure_owner = ''
  4810.             end
  4811.             else
  4812.             begin    /* If qualifier doesn't match current database */
  4813.                 raiserror 20001 '~~Rush_51~~'
  4814.                 return
  4815.             end
  4816.         end
  4817.     end
  4818.  
  4819.     if @procedure_name is null
  4820.     begin    /*    If procedure name not supplied, match all */
  4821.         select @procedure_name = '%'
  4822.     end
  4823.  
  4824.     /* first we need to extract the procedure group number, if one exists */
  4825.     select @semi_position = charindex(';',@procedure_name)
  4826.     if (@semi_position > 0)
  4827.     begin    /* If group number separator (;) found */
  4828.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4829.         select @group_num_upper = @group_num_lower
  4830.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4831.     end
  4832.     else
  4833.     begin    /* No group separator, so default to group number of 1 */
  4834.         select @group_num_lower = 1
  4835.         select @group_num_upper = 32767            
  4836.     end
  4837.  
  4838.     if @procedure_owner is null
  4839.     begin    /* If unqualified procedure name */
  4840.         SELECT @full_procedure_name = @procedure_name
  4841.     end
  4842.     else
  4843.     begin    /* Qualified procedure name */
  4844.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4845.     end
  4846.  
  4847.     /*    Get Object ID */
  4848.     SELECT @procedure_id = object_id(@full_procedure_name)
  4849.     if ((charindex('%',@full_procedure_name) = 0) and
  4850.         (charindex('_',@full_procedure_name) = 0) and
  4851.         @procedure_id <> 0)
  4852.     begin
  4853.         /* this block is for the case where there is no pattern
  4854.             matching required for the procedure name */
  4855.         SELECT
  4856.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4857.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4858.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4859.             COLUMN_NAME = convert(varchar(32),c.name),
  4860.             COLUMN_TYPE = convert(smallint, 0),
  4861.             d.DATA_TYPE,
  4862.             TYPE_NAME = t.name,
  4863.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4864.             LENGTH = isnull(d.length, convert(int,c.length)),
  4865.             SCALE = d.numeric_scale,
  4866.             d.RADIX,
  4867.             d.NULLABLE,
  4868.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4869.             COLUMN_DEF = convert(varchar(255),null),
  4870.             d.SQL_DATA_TYPE,
  4871.             d.SQL_DATETIME_SUB,
  4872.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4873.             ORDINAL_POSITION = convert(int,c.colid),
  4874.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4875.             SS_DATA_TYPE = c.type
  4876.         FROM
  4877.             syscolumns c,
  4878.             sysobjects o,
  4879.             master.dbo.spt_datatype_info d,
  4880.             systypes t
  4881.         WHERE
  4882.             o.id = @procedure_id
  4883.             AND c.id = o.id
  4884.             AND t.type = d.ss_dtype
  4885.             AND c.length = isnull(d.fixlen, c.length)
  4886.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4887.             AND c.usertype = t.usertype
  4888.             AND c.name like @column_name
  4889.             AND c.number between @group_num_lower and @group_num_upper
  4890.         UNION ALL
  4891.         SELECT           /* return value row*/
  4892.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4893.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4894.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4895.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4896.             COLUMN_TYPE = convert(smallint, 5),
  4897.             DATA_TYPE = convert(smallint, 4),
  4898.             TYPE_NAME = convert(varchar(32),'int'),
  4899.             "PRECISION" = convert(int,10),
  4900.             LENGTH = convert(int,4),
  4901.             SCALE = convert(smallint,0),
  4902.             RADIX = convert(smallint,10),
  4903.             NULLABLE = convert(smallint,0),
  4904.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4905.             COLUMN_DEF = convert(varchar(255),NULL),
  4906.             SQL_DATA_TYPE = convert(smallint, 4),
  4907.             SQL_DATETIME_SUB = convert(smallint,null),
  4908.             CHAR_OCTET_LENGTH = convert(int,null),
  4909.             ORDINAL_POSITION = convert(int,0),
  4910.             IS_NULLABLE = convert(varchar(254),'NO'),
  4911.             SS_DATA_TYPE = convert(tinyint,56)
  4912.         FROM
  4913.             syscomments c, sysobjects o
  4914.         WHERE
  4915.             o.id = @procedure_id
  4916.             AND c.id = o.id
  4917.             AND c.colid = 1
  4918.             AND o.type = 'P'                        /* Just Procedures */
  4919.             AND 'RETURN_VALUE' like @column_name
  4920.             AND c.number between @group_num_lower and @group_num_upper        
  4921.         ORDER BY 1, 2, 3, 18
  4922.     end
  4923.     else
  4924.     begin
  4925.         /* this block is for the case where there IS pattern
  4926.             matching done on the procedure name */
  4927.         if @procedure_owner is null
  4928.             select @procedure_owner = '%'
  4929.         SELECT
  4930.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4931.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4932.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4933.             COLUMN_NAME = convert(varchar(32),c.name),
  4934.             COLUMN_TYPE = convert(smallint, 0),
  4935.             d.DATA_TYPE,
  4936.             TYPE_NAME = t.name,
  4937.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4938.             LENGTH = isnull(d.length, convert(int,c.length)),
  4939.             SCALE = d.numeric_scale,
  4940.             d.RADIX,
  4941.             d.NULLABLE,
  4942.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4943.             COLUMN_DEF = convert(varchar(255),null),
  4944.             d.SQL_DATA_TYPE,
  4945.             d.SQL_DATETIME_SUB,
  4946.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4947.             ORDINAL_POSITION = convert(int,c.colid),
  4948.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4949.             SS_DATA_TYPE = c.type
  4950.         FROM
  4951.             syscolumns c,
  4952.             sysobjects o,
  4953.             master.dbo.spt_datatype_info d,
  4954.             systypes t
  4955.         WHERE
  4956.             o.name like @procedure_name
  4957.             AND user_name(o.uid) like @procedure_owner
  4958.             AND o.id = c.id
  4959.             AND t.type = d.ss_dtype
  4960.             AND c.length = isnull(d.fixlen, c.length)
  4961.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4962.             AND c.usertype = t.usertype
  4963.             AND o.type = 'P'                            /* Just Procedures */
  4964.             AND c.name like @column_name
  4965.             AND c.number between @group_num_lower and @group_num_upper
  4966.         UNION ALL
  4967.         SELECT           /* return value row*/
  4968.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4969.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4970.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4971.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4972.             COLUMN_TYPE = convert(smallint, 5),
  4973.             DATA_TYPE = convert(smallint, 4),
  4974.             TYPE_NAME = convert(varchar(32),'int'),
  4975.             "PRECISION" = convert(int,10),
  4976.             LENGTH = convert(int,4),
  4977.             SCALE = convert(smallint,0),
  4978.             RADIX = convert(smallint,10),
  4979.             NULLABLE = convert(smallint,0),
  4980.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4981.             COLUMN_DEF = convert(varchar(255),NULL),
  4982.             SQL_DATA_TYPE = convert(smallint, 4),
  4983.             SQL_DATETIME_SUB = convert(smallint,null),
  4984.             CHAR_OCTET_LENGTH = convert(int,null),
  4985.             ORDINAL_POSITION = convert(int,0),
  4986.             IS_NULLABLE = convert(varchar(254),'NO'),
  4987.             SS_DATA_TYPE = convert(tinyint,56)
  4988.         FROM
  4989.             syscomments c, sysobjects o
  4990.         WHERE
  4991.             o.name like @procedure_name
  4992.             AND user_name(o.uid) like @procedure_owner
  4993.             AND c.id = o.id
  4994.             AND c.colid = 1
  4995.             AND o.type = 'P'                        /* Just Procedures */
  4996.             AND 'RETURN_VALUE' like @column_name
  4997.             AND c.number between @group_num_lower and @group_num_upper
  4998.         ORDER BY 1, 2, 3, 18
  4999.     end
  5000. go
  5001.  
  5002. if (charindex('6.00', @@version) = 0 and
  5003.     charindex('6.50', @@version) = 0 and
  5004.     charindex('7.00', @@version) = 0)
  5005. begin
  5006.     print ''
  5007.     print ''
  5008.     print 'Warning:'
  5009.     print 'you are installing the stored procedures '
  5010.     print 'on a pre 6.0 SQL Server.'
  5011.     print 'Ignore the following error.'
  5012. end
  5013. else
  5014.     drop proc sp_sproc_columns
  5015. go
  5016.  
  5017. /*    Procedure for 6.0 and 6.50 servers */
  5018. CREATE PROCEDURE sp_sproc_columns (
  5019.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5020.                  @procedure_owner        varchar(96) = null,
  5021.                  @procedure_qualifier    varchar(32) = null,
  5022.                  @column_name            varchar(96) = null,
  5023.                  @ODBCVer                int = 2)
  5024. AS
  5025.     DECLARE @group_num_lower smallint
  5026.     DECLARE @group_num_upper smallint
  5027.     DECLARE @semi_position int
  5028.     DECLARE @full_procedure_name    varchar(205)
  5029.     DECLARE @procedure_id int
  5030.  
  5031.     if @column_name is null /*    If column name not supplied, match all */
  5032.         select @column_name = '%'
  5033.     if @procedure_qualifier is not null
  5034.     begin
  5035.         if db_name() <> @procedure_qualifier
  5036.         begin
  5037.             if @procedure_qualifier = ''
  5038.             begin
  5039.                 /* in this case, we need to return an empty result set */
  5040.                 /* because the user has requested a database with an empty name */
  5041.                 select @procedure_name = ''
  5042.                 select @procedure_owner = ''
  5043.             end
  5044.             else
  5045.             begin    /* If qualifier doesn't match current database */
  5046.                 raiserror (15250, -1,-1)
  5047.                 return
  5048.             end
  5049.         end
  5050.     end
  5051.  
  5052.     if @procedure_name is null
  5053.     begin    /*    If procedure name not supplied, match all */
  5054.         select @procedure_name = '%'
  5055.     end
  5056.  
  5057.     /* first we need to extract the procedure group number, if one exists */
  5058.     select @semi_position = charindex(';',@procedure_name)
  5059.     if (@semi_position > 0)
  5060.     begin    /* If group number separator (;) found */
  5061.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5062.         select @group_num_upper = @group_num_lower
  5063.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5064.     end
  5065.     else
  5066.     begin    /* No group separator, so default to group number of 1 */
  5067.         select @group_num_lower = 1
  5068.         select @group_num_upper = 32767            
  5069.     end
  5070.  
  5071.     if @procedure_owner is null
  5072.     begin    /* If unqualified procedure name */
  5073.         SELECT @full_procedure_name = @procedure_name
  5074.     end
  5075.     else
  5076.     begin    /* Qualified procedure name */
  5077.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5078.     end
  5079.  
  5080.     /*    Get Object ID */
  5081.     SELECT @procedure_id = object_id(@full_procedure_name)
  5082.     if ((charindex('%',@full_procedure_name) = 0) and
  5083.         (charindex('[',@full_procedure_name) = 0) and
  5084.         (charindex('_',@full_procedure_name) = 0) and
  5085.         @procedure_id <> 0)
  5086.     begin
  5087.         /* this block is for the case where there is no pattern
  5088.             matching required for the procedure name */
  5089.         SELECT
  5090.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5091.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5092.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5093.             COLUMN_NAME = convert(varchar(32),c.name),
  5094.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5095.             d.DATA_TYPE,
  5096.             TYPE_NAME = t.name,
  5097.             convert(int,case
  5098.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5099.                 else isnull(convert(int,c.prec), 2147483647)
  5100.             end) "PRECISION",
  5101.             convert(int,case
  5102.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5103.                     c.prec+2
  5104.                 else
  5105.                     isnull(d.length, c.length)
  5106.             end) LENGTH,
  5107.             SCALE = convert(smallint, c.scale),
  5108.             d.RADIX,
  5109.             d.NULLABLE,
  5110.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5111.             COLUMN_DEF = convert(varchar(255),NULL),
  5112.             d.SQL_DATA_TYPE,
  5113.             d.SQL_DATETIME_SUB,
  5114.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5115.             ORDINAL_POSITION = convert(int,c.colid),
  5116.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5117.             SS_DATA_TYPE = c.type
  5118.         FROM
  5119.             syscolumns c,
  5120.             sysobjects o,
  5121.             master.dbo.spt_datatype_info d,
  5122.             systypes t
  5123.         WHERE
  5124.             o.id = @procedure_id
  5125.             AND c.id = o.id
  5126.             AND c.type = d.ss_dtype
  5127.             AND c.length = isnull(d.fixlen, c.length)
  5128.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5129.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5130.             AND c.usertype = t.usertype
  5131.             AND c.name like @column_name
  5132.             AND c.number between @group_num_lower and @group_num_upper
  5133.         UNION ALL
  5134.         SELECT           /* return value row*/
  5135.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5136.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5137.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5138.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  5139.             COLUMN_TYPE = convert(smallint, 5),
  5140.             DATA_TYPE = convert(smallint, 4),
  5141.             TYPE_NAME = convert(varchar(32),'int'),
  5142.             "PRECISION" = convert(int,10),
  5143.             LENGTH = convert(int,4),
  5144.             SCALE = convert(smallint,0),
  5145.             RADIX = convert(smallint,10),
  5146.             NULLABLE = convert(smallint,0),
  5147.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5148.             COLUMN_DEF = convert(varchar(255),NULL),
  5149.             SQL_DATA_TYPE = convert(smallint, 4),
  5150.             SQL_DATETIME_SUB = convert(smallint,null),
  5151.             CHAR_OCTET_LENGTH = convert(int,null),
  5152.             ORDINAL_POSITION = convert(int,0),
  5153.             IS_NULLABLE = convert(varchar(254),'NO'),
  5154.             SS_DATA_TYPE = convert(tinyint,56)
  5155.         FROM
  5156.             syscomments c, sysobjects o
  5157.         WHERE
  5158.             o.id = @procedure_id
  5159.             AND c.id = o.id
  5160.             AND c.colid = 1
  5161.             AND o.type = 'P'                        /* Just Procedures */
  5162.             AND 'RETURN_VALUE' like @column_name
  5163.             AND c.number between @group_num_lower and @group_num_upper
  5164.         ORDER BY 1, 2, 3, 18
  5165.     end
  5166.     else
  5167.     begin
  5168.         /* this block is for the case where there IS pattern
  5169.             matching done on the procedure name */
  5170.         if @procedure_owner is null
  5171.             select @procedure_owner = '%'
  5172.         SELECT
  5173.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5174.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5175.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5176.             COLUMN_NAME = convert(varchar(32),c.name),
  5177.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5178.             d.DATA_TYPE,
  5179.             TYPE_NAME = t.name,
  5180.             convert(int,case
  5181.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5182.                 else isnull(convert(int,c.prec), 2147483647)
  5183.             end) "PRECISION",
  5184.             convert(int,case
  5185.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5186.                     c.prec+2
  5187.                 else
  5188.                     isnull(d.length, c.length)
  5189.             end) LENGTH,
  5190.             SCALE = convert(smallint, c.scale),
  5191.             d.RADIX,
  5192.             d.NULLABLE,
  5193.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5194.             COLUMN_DEF = convert(varchar(255),NULL),
  5195.             d.SQL_DATA_TYPE,
  5196.             d.SQL_DATETIME_SUB,
  5197.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5198.             ORDINAL_POSITION = convert(int,c.colid),
  5199.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5200.             SS_DATA_TYPE = c.type
  5201.         FROM
  5202.             syscolumns c,
  5203.             sysobjects o,
  5204.             master.dbo.spt_datatype_info d,
  5205.             systypes t
  5206.         WHERE
  5207.             o.name like @procedure_name
  5208.             AND user_name(o.uid) like @procedure_owner
  5209.             AND o.id = c.id
  5210.             AND c.type = d.ss_dtype
  5211.             AND c.length = isnull(d.fixlen, c.length)
  5212.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5213.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5214.             AND c.usertype = t.usertype
  5215.             AND o.type = 'P'                            /* Just Procedures */
  5216.             AND c.name like @column_name
  5217.             AND c.number between @group_num_lower and @group_num_upper
  5218.         UNION ALL
  5219.         SELECT           /* return value row*/
  5220.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5221.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5222.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5223.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  5224.             COLUMN_TYPE = convert(smallint, 5),
  5225.             DATA_TYPE = convert(smallint, 4),
  5226.             TYPE_NAME = convert(varchar(32),'int'),
  5227.             "PRECISION" = convert(int,10),
  5228.             LENGTH = convert(int,4),
  5229.             SCALE = convert(smallint,0),
  5230.             RADIX = convert(smallint,10),
  5231.             NULLABLE = convert(smallint,0),
  5232.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5233.             COLUMN_DEF = convert(varchar(255),NULL),
  5234.             SQL_DATA_TYPE = convert(smallint, 4),
  5235.             SQL_DATETIME_SUB = convert(smallint,null),
  5236.             CHAR_OCTET_LENGTH = convert(int,null),
  5237.             ORDINAL_POSITION = convert(int,0),
  5238.             IS_NULLABLE = convert(varchar(254),'NO'),
  5239.             SS_DATA_TYPE = convert(tinyint,56)
  5240.         FROM
  5241.             syscomments c, sysobjects o
  5242.         WHERE
  5243.             o.name like @procedure_name
  5244.             AND user_name(o.uid) like @procedure_owner
  5245.             AND c.id = o.id
  5246.             AND c.colid = 1
  5247.             AND o.type = 'P'                        /* Just Procedures */
  5248.             AND 'RETURN_VALUE' like @column_name
  5249.             AND c.number between @group_num_lower and @group_num_upper
  5250.         ORDER BY 1, 2, 3, 18
  5251.     end
  5252. go
  5253.  
  5254. if (charindex('7.00', @@version) = 0)
  5255. begin
  5256.     print ''
  5257.     print ''
  5258.     print 'Warning:'
  5259.     print 'you are installing the stored procedures '
  5260.     print 'on a pre 7.0 SQL Server.'
  5261.     print 'Ignore the following errors.'
  5262. end
  5263. else
  5264.     drop proc sp_sproc_columns
  5265. go
  5266.  
  5267. /*    Procedure for 7.0 server */
  5268. CREATE PROCEDURE sp_sproc_columns (
  5269.                  @procedure_name        nvarchar(390) = '%',
  5270.                  @procedure_owner        nvarchar(384) = null,
  5271.                  @procedure_qualifier    sysname = null,
  5272.                  @column_name            nvarchar(384) = null,
  5273.                  @ODBCVer                int = 2)
  5274. AS
  5275.     DECLARE @group_num_lower smallint
  5276.     DECLARE @group_num_upper smallint
  5277.     DECLARE @semi_position int
  5278.     DECLARE @full_procedure_name    nvarchar(774)
  5279.     DECLARE @procedure_id int
  5280.  
  5281.     if @column_name is null /*    If column name not supplied, match all */
  5282.         select @column_name = '%'
  5283.     if @procedure_qualifier is not null
  5284.     begin
  5285.         if db_name() <> @procedure_qualifier
  5286.         begin
  5287.             if @procedure_qualifier = ''
  5288.             begin
  5289.                 /* in this case, we need to return an empty result set */
  5290.                 /* because the user has requested a database with an empty name */
  5291.                 select @procedure_name = ''
  5292.                 select @procedure_owner = ''
  5293.             end
  5294.             else
  5295.             begin    /* If qualifier doesn't match current database */
  5296.                 raiserror (15250, -1,-1)
  5297.                 return
  5298.             end
  5299.         end
  5300.     end
  5301.  
  5302.     if @procedure_name is null
  5303.     begin    /*    If procedure name not supplied, match all */
  5304.         select @procedure_name = '%'
  5305.     end
  5306.  
  5307.     /* first we need to extract the procedure group number, if one exists */
  5308.     select @semi_position = charindex(';',@procedure_name)
  5309.     if (@semi_position > 0)
  5310.     begin    /* If group number separator (;) found */
  5311.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5312.         select @group_num_upper = @group_num_lower
  5313.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5314.     end
  5315.     else
  5316.     begin    /* No group separator, so default to all groups */
  5317.         select @group_num_lower = 1
  5318.         select @group_num_upper = 32767            
  5319.     end
  5320.  
  5321.     if @procedure_owner is null
  5322.     begin    /* If unqualified procedure name */
  5323.         SELECT @full_procedure_name = quotename(@procedure_name)
  5324.     end
  5325.     else
  5326.     begin    /* Qualified procedure name */
  5327.         if @procedure_owner = ''
  5328.         begin    /* If empty owner name */
  5329.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5330.         end
  5331.         else
  5332.         begin
  5333.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5334.                 '.' + quotename(@procedure_name)
  5335.         end
  5336.     end
  5337.  
  5338.     /*    Get Object ID */
  5339.     SELECT @procedure_id = object_id(@full_procedure_name)
  5340.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5341.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5342.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5343.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5344.         not (@procedure_id is null))
  5345.     begin
  5346.         /* this block is for the case where there is no pattern
  5347.             matching required for the procedure name */
  5348.         SELECT
  5349.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5350.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5351.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5352.             COLUMN_NAME = convert(sysname,c.name),
  5353.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5354.             d.DATA_TYPE,
  5355.             TYPE_NAME = t.name,
  5356.             convert(int,case
  5357.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5358.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5359.             end) "PRECISION",
  5360.             convert(int,case
  5361.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5362.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5363.                 else
  5364.                     isnull(d.length, c.length)
  5365.             end) LENGTH,
  5366.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5367.             d.RADIX,
  5368.             d.NULLABLE,
  5369.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5370.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5371.             d.SQL_DATA_TYPE,
  5372.             d.SQL_DATETIME_SUB,
  5373.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5374.             ORDINAL_POSITION = convert(int,c.colid),
  5375.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5376.             SS_DATA_TYPE = c.type
  5377.         FROM
  5378.             syscolumns c,
  5379.             sysobjects o,
  5380.             master.dbo.spt_datatype_info d,
  5381.             systypes t
  5382.         WHERE
  5383.             o.id = @procedure_id
  5384.             AND c.id = o.id
  5385.             AND c.xtype = d.ss_dtype
  5386.             AND c.length = isnull(d.fixlen, c.length)
  5387.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5388.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5389.             AND c.xusertype = t.xusertype
  5390.             AND c.name like @column_name
  5391.             AND c.number between @group_num_lower and @group_num_upper
  5392.         UNION ALL
  5393.         SELECT           /* return value row*/
  5394.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5395.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5396.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5397.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  5398.             COLUMN_TYPE = convert(smallint, 5),
  5399.             DATA_TYPE = convert(smallint, 4),
  5400.             TYPE_NAME = convert(sysname,'int'),
  5401.             "PRECISION" = convert(int,10),
  5402.             LENGTH = convert(int,4),
  5403.             SCALE = convert(smallint,0),
  5404.             RADIX = convert(smallint,10),
  5405.             NULLABLE = convert(smallint,0),
  5406.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5407.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5408.             SQL_DATA_TYPE = convert(smallint, 4),
  5409.             SQL_DATETIME_SUB = convert(smallint,null),
  5410.             CHAR_OCTET_LENGTH = convert(int,null),
  5411.             ORDINAL_POSITION = convert(int,0),
  5412.             IS_NULLABLE = convert(varchar(254),'NO'),
  5413.             SS_DATA_TYPE = convert(tinyint,56)
  5414.         FROM
  5415.             syscomments c, sysobjects o
  5416.         WHERE
  5417.             o.id = @procedure_id
  5418.             AND c.id = o.id
  5419.             AND c.colid = 1
  5420.             AND o.type = 'P'                        /* Just Procedures */
  5421.             AND 'RETURN_VALUE' like @column_name
  5422.             AND c.number between @group_num_lower and @group_num_upper        
  5423.         ORDER BY 1, 2, 3, 18
  5424.     end
  5425.     else
  5426.     begin
  5427.         /* this block is for the case where there IS pattern
  5428.             matching done on the procedure name */
  5429.         if @procedure_owner is null
  5430.             select @procedure_owner = '%'
  5431.         SELECT
  5432.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5433.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5434.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5435.             COLUMN_NAME = convert(sysname,c.name),
  5436.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  5437.             d.DATA_TYPE,
  5438.             TYPE_NAME = t.name,
  5439.             convert(int,case
  5440.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5441.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5442.             end) "PRECISION",
  5443.             convert(int,case
  5444.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5445.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5446.                 else
  5447.                     isnull(d.length, c.length)
  5448.             end) LENGTH,
  5449.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5450.             d.RADIX,
  5451.             d.NULLABLE,
  5452.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5453.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5454.             d.SQL_DATA_TYPE,
  5455.             d.SQL_DATETIME_SUB,
  5456.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5457.             ORDINAL_POSITION = convert(int,c.colid),
  5458.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5459.             SS_DATA_TYPE = c.type
  5460.         FROM
  5461.             syscolumns c,
  5462.             sysobjects o,
  5463.             master.dbo.spt_datatype_info d,
  5464.             systypes t
  5465.         WHERE
  5466.             o.name like @procedure_name
  5467.             AND user_name(o.uid) like @procedure_owner
  5468.             AND o.id = c.id
  5469.             AND c.xtype = d.ss_dtype
  5470.             AND c.length = isnull(d.fixlen, c.length)
  5471.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5472.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5473.             AND c.xusertype = t.xusertype
  5474.             AND o.type = 'P'                            /* Just Procedures */
  5475.             AND c.name like @column_name
  5476.             AND c.number between @group_num_lower and @group_num_upper
  5477.         UNION ALL
  5478.         SELECT           /* return value row*/
  5479.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5480.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5481.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5482.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  5483.             COLUMN_TYPE = convert(smallint, 5),
  5484.             DATA_TYPE = convert(smallint, 4),
  5485.             TYPE_NAME = convert(sysname,'int'),
  5486.             "PRECISION" = convert(int,10),
  5487.             LENGTH = convert(int,4),
  5488.             SCALE = convert(smallint,0),
  5489.             RADIX = convert(smallint,10),
  5490.             NULLABLE = convert(smallint,0),
  5491.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5492.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5493.             SQL_DATA_TYPE = convert(smallint, 4),
  5494.             SQL_DATETIME_SUB = convert(smallint,null),
  5495.             CHAR_OCTET_LENGTH = convert(int,null),
  5496.             ORDINAL_POSITION = convert(int,0),
  5497.             IS_NULLABLE = convert(varchar(254),'NO'),
  5498.             SS_DATA_TYPE = convert(tinyint,56)
  5499.         FROM
  5500.             syscomments c, sysobjects o
  5501.         WHERE
  5502.             o.name like @procedure_name
  5503.             AND c.id = o.id
  5504.             AND user_name(o.uid) like @procedure_owner
  5505.             AND c.colid = 1
  5506.             AND o.type = 'P'                        /* Just Procedures */
  5507.             AND 'RETURN_VALUE' like @column_name
  5508.             AND c.number between @group_num_lower and @group_num_upper
  5509.         ORDER BY 1, 2, 3, 18
  5510.     end
  5511. go
  5512.  
  5513. grant execute on sp_sproc_columns to public
  5514. go
  5515.  
  5516. dump tran master with no_log
  5517. go
  5518.  
  5519. print 'creating sp_statistics'
  5520. go
  5521.  
  5522. /*    Procedure for pre-7.0 server */
  5523. CREATE PROCEDURE sp_statistics (
  5524.                  @table_name        varchar(32),
  5525.                  @table_owner        varchar(32) = null,
  5526.                  @table_qualifier    varchar(32) = null,
  5527.                  @index_name        varchar(32) = '%',
  5528.                  @is_unique         char(1) = 'N',
  5529.                  @accuracy            char(1) = 'Q')
  5530. AS
  5531.     set nocount on
  5532.     DECLARE @indid                int
  5533.     DECLARE @lastindid            int
  5534.     DECLARE @table_id            int
  5535.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  5536.  
  5537.     create table #TmpIndex(
  5538.         TABLE_QUALIFIER varchar(32) NULL,
  5539.         TABLE_OWNER     varchar(32) NULL,
  5540.         TABLE_NAME        varchar(32) NOT NULL,
  5541.         INDEX_QUALIFIER varchar(32) null,
  5542.         INDEX_NAME        varchar(32) null,
  5543.         NON_UNIQUE        smallint null,
  5544.         TYPE            smallint NOT NULL,
  5545.         SEQ_IN_INDEX    smallint null,
  5546.         COLUMN_NAME     varchar(32) null,
  5547.         COLLATION        char(1) null,
  5548.         index_id        int null,
  5549.         CARDINALITY     int null,
  5550.         PAGES            int null,
  5551.         status            smallint NOT NULL)
  5552.  
  5553.     if @table_qualifier is not null
  5554.     begin
  5555.         if db_name() <> @table_qualifier
  5556.         begin    /* If qualifier doesn't match current database */
  5557.             raiserror 20001 '~~Rush_5~~'
  5558.             return
  5559.         end
  5560.     end
  5561.  
  5562.     if @accuracy not in ('Q','E')
  5563.         begin
  5564.             raiserror 20002 '~~Rush_58~~'
  5565.             return
  5566.         end
  5567.  
  5568.     if (@@trancount <> 0 and
  5569.         charindex('6.50', @@version) = 0 and
  5570.         charindex('7.00', @@version) = 0)
  5571.     begin    /* If inside a transaction */
  5572.         raiserror 20003 '~~Rush_59~~'
  5573.         return
  5574.     end
  5575.  
  5576.     if @table_owner is null
  5577.     begin    /* If unqualified table name */
  5578.         SELECT @full_table_name = @table_name
  5579.     end
  5580.     else
  5581.     begin    /* Qualified table name */
  5582.         if @table_owner = ''
  5583.         begin    /* If empty owner name */
  5584.             SELECT @full_table_name = @table_owner
  5585.         end
  5586.         else
  5587.         begin
  5588.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5589.         end
  5590.     end
  5591.     /*    Get Object ID */
  5592.     SELECT @table_id = object_id(@full_table_name)
  5593.  
  5594.     /*    Start at lowest index id */
  5595.     SELECT @indid = min(indid)
  5596.     FROM sysindexes
  5597.     WHERE id = @table_id
  5598.         AND indid > 0
  5599.         AND indid < 255
  5600.  
  5601.     WHILE @indid is not NULL
  5602.     BEGIN
  5603.         INSERT #TmpIndex    /* Add all columns that are in index */
  5604.             SELECT
  5605.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5606.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5607.                 o.name,                                 /* TABLE_NAME       */
  5608.                 o.name,                                 /* INDEX_QUALIFIER */
  5609.                 x.name,                                 /* INDEX_NAME       */
  5610.                 0,                                        /* NON_UNIQUE       */
  5611.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5612.                 colid,                                    /* SEQ_IN_INDEX    */
  5613.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5614.                 'A',                                    /* COLLATION       */
  5615.                 @indid,                                 /* index_id        */
  5616.                 x.rows,                                 /* CARDINALITY       */
  5617.                 x.dpages,                                /* PAGES           */
  5618.                 x.status                                /* status            */
  5619.             FROM sysindexes x, syscolumns c, sysobjects o
  5620.             WHERE
  5621.                 x.id = @table_id
  5622.                 AND x.id = o.id
  5623.                 AND x.id = c.id
  5624.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  5625.                 AND x.indid = @indid
  5626.         /*
  5627.         **      Now move @indid to the next index.
  5628.         */
  5629.         SELECT @lastindid = @indid
  5630.         SELECT @indid = NULL
  5631.  
  5632.         SELECT @indid = min(indid)
  5633.         FROM sysindexes
  5634.         WHERE id = @table_id
  5635.             AND indid > @lastindid
  5636.             AND indid < 255
  5637.     END
  5638.  
  5639.     UPDATE #TmpIndex
  5640.         SET NON_UNIQUE = 1
  5641.         WHERE status&2 <> 2 /* If non-unique index */
  5642.     UPDATE #TmpIndex
  5643.         SET
  5644.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5645.             CARDINALITY = NULL,
  5646.             PAGES = NULL
  5647.         WHERE index_id > 1    /* If non-clustered index */
  5648.  
  5649.     /* now add row for table statistics */
  5650.     INSERT #TmpIndex
  5651.         SELECT
  5652.             DB_NAME(),                /* TABLE_QUALIFIER */
  5653.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5654.             o.name,                 /* TABLE_NAME       */
  5655.             null,                    /* INDEX_QUALIFIER */
  5656.             null,                    /* INDEX_NAME       */
  5657.             null,                    /* NON_UNIQUE       */
  5658.             0,                        /* SQL_TABLE_STAT  */
  5659.             null,                    /* SEQ_IN_INDEX    */
  5660.             null,                    /* COLUMN_NAME       */
  5661.             null,                    /* COLLATION       */
  5662.             0,                        /* index_id        */
  5663.             x.rows,                 /* CARDINALITY       */
  5664.             x.dpages,                /* PAGES           */
  5665.             0                        /* status           */
  5666.         FROM sysindexes x, sysobjects o
  5667.         WHERE o.id = @table_id
  5668.             AND x.id = o.id
  5669.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5670.                                                 /*    then table stats are in */
  5671.                                                 /*    a row with indid =0        */
  5672.  
  5673.     if @is_unique <> 'Y'    /* If all indexes desired */
  5674.         SELECT
  5675.             TABLE_QUALIFIER,
  5676.             TABLE_OWNER,
  5677.             TABLE_NAME,
  5678.             NON_UNIQUE,
  5679.             INDEX_QUALIFIER,
  5680.             INDEX_NAME,
  5681.             TYPE,
  5682.             SEQ_IN_INDEX,
  5683.             COLUMN_NAME,
  5684.             COLLATION,
  5685.             CARDINALITY,
  5686.             PAGES,
  5687.             FILTER_CONDITION = convert(varchar(128),null)
  5688.         FROM #TmpIndex
  5689.         WHERE
  5690.             INDEX_NAME like @index_name /* If matching name */
  5691.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5692.         ORDER BY 4, 7, 6, 8
  5693.     else                    /* If only unique indexes desired */
  5694.         SELECT
  5695.             TABLE_QUALIFIER,
  5696.             TABLE_OWNER,
  5697.             TABLE_NAME,
  5698.             NON_UNIQUE,
  5699.             INDEX_QUALIFIER,
  5700.             INDEX_NAME,
  5701.             TYPE,
  5702.             SEQ_IN_INDEX,
  5703.             COLUMN_NAME,
  5704.             COLLATION,
  5705.             CARDINALITY,
  5706.             PAGES,
  5707.             FILTER_CONDITION = convert(varchar(128),null)
  5708.         FROM #TmpIndex
  5709.         WHERE
  5710.             (NON_UNIQUE = 0             /* If unique */
  5711.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5712.             and (INDEX_NAME like @index_name    /* If matching name */
  5713.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5714.         ORDER BY 4, 7, 6, 8
  5715.  
  5716.     DROP TABLE #TmpIndex
  5717. go
  5718.  
  5719. if (charindex('7.00', @@version) = 0)
  5720. begin
  5721.     print ''
  5722.     print ''
  5723.     print 'Warning:'
  5724.     print 'you are installing the stored procedures '
  5725.     print 'on a pre 7.0 SQL Server.'
  5726.     print 'Ignore the following errors.'
  5727. end
  5728. else
  5729.     drop proc sp_statistics
  5730. go
  5731.  
  5732. /*    Procedure for 7.0 server */
  5733. CREATE PROCEDURE sp_statistics (
  5734.                  @table_name        sysname,
  5735.                  @table_owner        sysname = null,
  5736.                  @table_qualifier    sysname = null,
  5737.                  @index_name        sysname = '%',
  5738.                  @is_unique         char(1) = 'N',
  5739.                  @accuracy            char(1) = 'Q')
  5740. AS
  5741.     set nocount on
  5742.     DECLARE @indid                int
  5743.     DECLARE @lastindid            int
  5744.     DECLARE @table_id            int
  5745.     DECLARE @full_table_name    nvarchar(257)
  5746.  
  5747.     create table #TmpIndex(
  5748.         TABLE_QUALIFIER sysname NULL,
  5749.         TABLE_OWNER     sysname NULL,
  5750.         TABLE_NAME        sysname NOT NULL,
  5751.         INDEX_QUALIFIER sysname null,
  5752.         INDEX_NAME        sysname null,
  5753.         NON_UNIQUE        smallint null,
  5754.         TYPE            smallint NOT NULL,
  5755.         SEQ_IN_INDEX    smallint null,
  5756.         COLUMN_NAME     sysname null,
  5757.         COLLATION        char(1) null,
  5758.         index_id        int null,
  5759.         CARDINALITY     int null,
  5760.         PAGES            int null,
  5761.         status            int NOT NULL)
  5762.  
  5763.     if @table_qualifier is not null
  5764.     begin
  5765.         if db_name() <> @table_qualifier
  5766.         begin    /* If qualifier doesn't match current database */
  5767.             raiserror (15250, -1,-1)
  5768.             return
  5769.         end
  5770.     end
  5771.  
  5772.     if @accuracy not in ('Q','E')
  5773.         begin
  5774.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  5775.             return
  5776.         end
  5777.  
  5778.     if @table_owner is null
  5779.     begin    /* If unqualified table name */
  5780.         SELECT @full_table_name = quotename(@table_name)
  5781.     end
  5782.     else
  5783.     begin    /* Qualified table name */
  5784.         if @table_owner = ''
  5785.         begin    /* If empty owner name */
  5786.             SELECT @full_table_name = quotename(@table_owner)
  5787.         end
  5788.         else
  5789.         begin
  5790.             SELECT @full_table_name = quotename(@table_owner) +
  5791.                 '.' + quotename(@table_name)
  5792.         end
  5793.     end
  5794.     /*    Get Object ID */
  5795.     SELECT @table_id = object_id(@full_table_name)
  5796.  
  5797.     /*    Start at lowest index id */
  5798.     SELECT @indid = min(indid)
  5799.     FROM sysindexes
  5800.     WHERE not (@table_id is null)
  5801.         AND id = @table_id
  5802.         AND indid > 0
  5803.         AND indid < 255
  5804.  
  5805.     WHILE @indid is not NULL
  5806.     BEGIN
  5807.         INSERT #TmpIndex    /* Add all columns that are in index */
  5808.             SELECT
  5809.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5810.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5811.                 o.name,                                 /* TABLE_NAME       */
  5812.                 o.name,                                 /* INDEX_QUALIFIER */
  5813.                 x.name,                                 /* INDEX_NAME       */
  5814.                 case                                    /* NON_UNIQUE       */
  5815.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  5816.                     else 0                                /* Unique index    */
  5817.                 end,
  5818.                 case                                    /* TYPE            */
  5819.                     when @indid > 1 then 3                /* Non-Clustered   */
  5820.                     else 1                                /* Clustered index */
  5821.                 end,
  5822.                 colid,                                    /* SEQ_IN_INDEX    */
  5823.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5824.                 'A',                                    /* COLLATION       */
  5825.                 @indid,                                 /* index_id        */
  5826.                 case                                    /* CARDINALITY       */
  5827.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5828.                     else x.rows                         /* Clustered index */
  5829.                 end,
  5830.                 case                                    /* PAGES           */
  5831.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5832.                     else x.dpages                        /* Clustered index */
  5833.                 end,
  5834.                 x.status                                /* status            */
  5835.             FROM sysindexes x, syscolumns c, sysobjects o
  5836.             WHERE
  5837.                 not (@table_id is null)
  5838.                 AND x.id = @table_id
  5839.                 AND x.id = o.id
  5840.                 AND x.id = c.id
  5841.                 AND colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5842.                 AND INDEX_COL(@full_table_name,indid,colid) IS NOT NULL
  5843.                 AND indid = @indid
  5844.                 AND (x.status&2 = 2
  5845.                     OR @is_unique <> 'Y')
  5846.                 AND (x.status&32) = 0
  5847.         /*
  5848.         **      Now move @indid to the next index.
  5849.         */
  5850.         SELECT @lastindid = @indid
  5851.         SELECT @indid = NULL
  5852.  
  5853.         SELECT @indid = min(indid)
  5854.         FROM sysindexes
  5855.         WHERE not (@table_id is null)
  5856.             AND id = @table_id
  5857.             AND indid > @lastindid
  5858.             AND indid < 255
  5859.     END
  5860.  
  5861.     /* now add row for table statistics */
  5862.     INSERT #TmpIndex
  5863.         SELECT
  5864.             DB_NAME(),                /* TABLE_QUALIFIER */
  5865.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5866.             o.name,                 /* TABLE_NAME       */
  5867.             null,                    /* INDEX_QUALIFIER */
  5868.             null,                    /* INDEX_NAME       */
  5869.             null,                    /* NON_UNIQUE       */
  5870.             0,                        /* SQL_TABLE_STAT  */
  5871.             null,                    /* SEQ_IN_INDEX    */
  5872.             null,                    /* COLUMN_NAME       */
  5873.             null,                    /* COLLATION       */
  5874.             0,                        /* index_id        */
  5875.             x.rows,                 /* CARDINALITY       */
  5876.             x.dpages,                /* PAGES           */
  5877.             0                        /* status           */
  5878.         FROM sysindexes x, sysobjects o
  5879.         WHERE not (@table_id is null)
  5880.             AND o.id = @table_id
  5881.             AND x.id = o.id
  5882.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5883.                                                 /*    then table stats are in */
  5884.                                                 /*    a row with indid =0        */
  5885.  
  5886.     SELECT
  5887.         TABLE_QUALIFIER,
  5888.         TABLE_OWNER,
  5889.         TABLE_NAME,
  5890.         NON_UNIQUE,
  5891.         INDEX_QUALIFIER,
  5892.         INDEX_NAME,
  5893.         TYPE,
  5894.         SEQ_IN_INDEX,
  5895.         COLUMN_NAME,
  5896.         COLLATION,
  5897.         CARDINALITY,
  5898.         PAGES,
  5899.         FILTER_CONDITION = convert(varchar(128),null)
  5900.     FROM #TmpIndex
  5901.     WHERE
  5902.         INDEX_NAME like @index_name /* If matching name */
  5903.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5904.     ORDER BY 4, 7, 6, 8
  5905.  
  5906.     DROP TABLE #TmpIndex
  5907. go
  5908.  
  5909. grant execute on sp_statistics to public
  5910. go
  5911.  
  5912. dump tran master with no_log
  5913. go
  5914.  
  5915. print 'creating sp_stored_procedures'
  5916. go
  5917.  
  5918.  
  5919. /* pre 7.00 version */
  5920. create procedure sp_stored_procedures(
  5921.                         @sp_name        varchar(102) = null,
  5922.                         @sp_owner        varchar(96) = null,
  5923.                         @sp_qualifier    varchar(32) = null)
  5924. as
  5925.     declare @proc_type smallint
  5926.  
  5927.     if @sp_qualifier is not null
  5928.     begin
  5929.         if db_name() <> @sp_qualifier
  5930.         begin
  5931.             if @sp_qualifier = ''
  5932.             begin
  5933.                 /* in this case, we need to return an empty result set */
  5934.                 /* because the user has requested a database with an empty name */
  5935.                 select @sp_name = ''
  5936.                 select @sp_owner = ''
  5937.             end else
  5938.             begin    /* If qualifier doesn't match current database */
  5939.                 raiserror 20001 '~~Rush_51~~'
  5940.                 return
  5941.             end
  5942.         end
  5943.     end
  5944.  
  5945.     if @sp_name is null
  5946.     begin  /*  If procedure name not supplied, match all */
  5947.         select @sp_name = '%'
  5948.     end
  5949.     else begin
  5950.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5951.         begin
  5952.             if exists (select * from sysobjects
  5953.                 where uid = user_id()
  5954.                     and name = @sp_name
  5955.                     and type = 'P') /* Object type of Procedure */
  5956.             begin
  5957.                 select @sp_owner = user_name()
  5958.             end
  5959.         end
  5960.     end
  5961.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5962.         select @sp_owner = '%'
  5963.  
  5964.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5965.  
  5966.     select
  5967.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  5968.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  5969.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5970.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5971.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5972.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5973.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5974.         PROCEDURE_TYPE = @proc_type
  5975.     from
  5976.         sysobjects o,syscomments c,sysusers u
  5977.     where
  5978.         o.name like @sp_name
  5979.         and c.colid = 1
  5980.         and user_name(o.uid) like @sp_owner
  5981.         and o.type = 'P'        /* Object type of Procedure */
  5982.         and c.id = o.id
  5983.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  5984.         and (suser_id() = 1     /* User is the System Administrator */
  5985.             or o.uid = user_id()    /* User created the object */
  5986.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  5987.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  5988.              from sysprotects p
  5989.              /* outer join to correlate with all rows in sysobjects */
  5990.              where p.id =* o.id
  5991.                  /*  get rows for public,current user,user's group */
  5992.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  5993.                  /* check for SELECT,EXECUTE privilege */
  5994.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5995.             ) = 1     /* final magic...compare Grants    */
  5996.         )
  5997.     order by 1, 2, 3
  5998. go
  5999.  
  6000. grant execute on sp_stored_procedures to public
  6001. go
  6002.  
  6003.  
  6004. if (charindex('7.00', @@version) > 0)
  6005.     drop procedure sp_stored_procedures
  6006. else
  6007. begin
  6008.     print ''
  6009.     print ''
  6010.     print 'Warning:'
  6011.     print 'you are installing the stored procedures '
  6012.     print 'on a pre 7.0 SQL Server.'
  6013.     print 'Ignore the following errors.'
  6014. end
  6015. go
  6016.  
  6017.  
  6018. /* 7.00 version */
  6019. create procedure sp_stored_procedures(
  6020.                         @sp_name        nvarchar(390) = null,
  6021.                         @sp_owner        nvarchar(384) = null,
  6022.                         @sp_qualifier    sysname = null)
  6023. as
  6024.     declare @proc_type smallint
  6025.  
  6026.     if @sp_qualifier is not null
  6027.     begin
  6028.         if db_name() <> @sp_qualifier
  6029.         begin
  6030.             if @sp_qualifier = ''
  6031.             begin
  6032.                 /* in this case, we need to return an empty result set */
  6033.                 /* because the user has requested a database with an empty name */
  6034.                 select @sp_name = ''
  6035.                 select @sp_owner = ''
  6036.             end else
  6037.             begin    /* If qualifier doesn't match current database */
  6038.                 raiserror (15250, -1,-1)
  6039.                 return
  6040.             end
  6041.         end
  6042.     end
  6043.  
  6044.     if @sp_name is null
  6045.     begin  /*  If procedure name not supplied, match all */
  6046.         select @sp_name = '%'
  6047.     end
  6048.     else begin
  6049.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  6050.         begin
  6051.             if exists (select * from sysobjects
  6052.                 where uid = user_id()
  6053.                     and name = @sp_name
  6054.                     and type = 'P') /* Object type of Procedure */
  6055.             begin
  6056.                 select @sp_owner = user_name()
  6057.             end
  6058.         end
  6059.     end
  6060.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  6061.         select @sp_owner = '%'
  6062.  
  6063.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  6064.  
  6065.     select
  6066.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  6067.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  6068.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6069.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  6070.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  6071.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  6072.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6073.         PROCEDURE_TYPE = @proc_type
  6074.     from
  6075.         sysobjects o,syscomments c
  6076.     where
  6077.         o.name like @sp_name
  6078.         and c.colid = 1
  6079.         and user_name(o.uid) like @sp_owner
  6080.         and o.type = 'P'        /* Object type of Procedure */
  6081.         and c.id = o.id
  6082.         and permissions (o.id)&32 <> 0
  6083.     order by 1, 2, 3
  6084. go
  6085.  
  6086. grant execute on sp_stored_procedures to public
  6087. go
  6088.  
  6089. dump tran master with no_log
  6090. go
  6091.  
  6092.  
  6093. print 'creating sp_table_privileges'
  6094. go
  6095.  
  6096. /*    Procedure for pre 6.50 server */
  6097. CREATE PROCEDURE sp_table_privileges (
  6098.             @table_name         varchar(90),
  6099.             @table_owner        varchar(90) = null,
  6100.             @table_qualifier    varchar(32) = null)
  6101. as
  6102.     set nocount on
  6103.  
  6104.     declare @table_id    int,
  6105.             @owner_id     int,
  6106.             @full_table_name char(181)
  6107.     declare @refconst int
  6108.  
  6109.     select @refconst = 1
  6110.     if    (charindex('6.00', @@version) = 0)
  6111.         select @refconst = NULL
  6112.  
  6113.     if @table_qualifier is not null
  6114.     begin
  6115.         if db_name() != @table_qualifier
  6116.         begin    /* If qualifier doesn't match current database */
  6117.             raiserror 20001 'Table qualifier must be name of current database'
  6118.             return
  6119.         end
  6120.     end
  6121.     if @table_owner is null
  6122.     begin    /* If unqualified table name */
  6123.         SELECT @full_table_name = @table_name
  6124.     end
  6125.     else
  6126.     begin    /* Qualified table name */
  6127.         SELECT @full_table_name = @table_owner + '.' + @table_name
  6128.     end
  6129.     /*    Get Object ID */
  6130.     SELECT @table_id = object_id(@full_table_name)
  6131.  
  6132.     if @@trancount != 0
  6133.     begin    /* If inside a transaction */
  6134.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  6135.         return
  6136.     end
  6137.     create table #table_priv1(
  6138.         table_qualifier            varchar(32) NOT NULL,
  6139.         table_owner             varchar(32) NOT NULL,
  6140.         table_name                varchar(32) NOT NULL,
  6141.         grantor                 varchar(32) NOT NULL,
  6142.         grantee                 varchar(32) NOT NULL,
  6143.         select_privilege        int NOT NULL,
  6144.         insert_privilege        int NOT NULL,
  6145.         update_privilege        int NOT NULL,
  6146.         delete_privilege        int NOT NULL,
  6147.         references_privilege    int NULL,
  6148.         is_grantable            varchar(3) NOT NULL,
  6149.         uid                     int NOT NULL,
  6150.         gid                     int NOT NULL)
  6151.  
  6152.     insert into #table_priv1
  6153.         select distinct
  6154.             db_name(),
  6155.             user_name(o.uid),
  6156.             o.name,
  6157.             user_name(o.uid),
  6158.             u.name,
  6159.             0,
  6160.             0,
  6161.             0,
  6162.             0,
  6163.             @refconst,
  6164.             'no',
  6165.             u.uid,
  6166.             u.gid
  6167.         from sysusers u, sysobjects o
  6168.         where o.id = @table_id and u.uid != u.gid
  6169.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  6170.                                            ** user tables, and views. */
  6171.  
  6172.     /*
  6173.     ** now add row for table owner
  6174.     */
  6175.     if exists (
  6176.         select *
  6177.             from #table_priv1
  6178.             where grantor = grantee)
  6179.     begin
  6180.         update #table_priv1
  6181.         set
  6182.             select_privilege = 1,
  6183.             update_privilege = 1,
  6184.             insert_privilege = 1,
  6185.             delete_privilege = 1,
  6186.             references_privilege = 1,
  6187.             is_grantable = 'yes'
  6188.         where grantor = grantee
  6189.     end
  6190.     else
  6191.     begin
  6192.         insert into #table_priv1
  6193.             select    db_name(),
  6194.                 user_name(o.uid),
  6195.                 o.name,
  6196.                 user_name(o.uid),
  6197.                 user_name(o.uid),
  6198.                 1,
  6199.                 1,
  6200.                 1,
  6201.                 1,
  6202.                 @refconst,
  6203.                 'yes',
  6204.                 o.uid,
  6205.                 u.gid
  6206.             from sysobjects o, sysusers u
  6207.             where o.id = @table_id and u.uid = o.uid
  6208.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  6209.                                            ** user tables, and views. */
  6210.  
  6211.     end
  6212.  
  6213.     update #table_priv1
  6214.     set select_privilege = 1
  6215.     where
  6216.         exists (
  6217.             select * from sysprotects
  6218.             where
  6219.                 id = @table_id
  6220.                 and (#table_priv1.uid = uid
  6221.                     or #table_priv1.gid = uid
  6222.                     or uid = 0)
  6223.                 and protecttype = 205
  6224.                 and action = 193)
  6225.         and not exists (
  6226.             select * from sysprotects
  6227.             where
  6228.                 id = @table_id
  6229.                 and (#table_priv1.uid = uid
  6230.                     or #table_priv1.gid = uid
  6231.                     or uid = 0)
  6232.                 and protecttype = 206
  6233.                 and action = 193)
  6234.  
  6235.     update #table_priv1
  6236.     set insert_privilege = 1
  6237.     where
  6238.         exists (
  6239.             select * from sysprotects
  6240.             where
  6241.                 id = @table_id
  6242.                 and (#table_priv1.uid = uid
  6243.                     or #table_priv1.gid = uid
  6244.                     or uid = 0)
  6245.                 and protecttype = 205
  6246.                 and action = 195)
  6247.         and not exists (
  6248.             select * from sysprotects
  6249.             where
  6250.                 id = @table_id
  6251.                 and (#table_priv1.uid = uid
  6252.                     or #table_priv1.gid = uid
  6253.                     or uid = 0)
  6254.                 and protecttype = 206
  6255.                 and action = 195)
  6256.  
  6257.     update #table_priv1
  6258.     set delete_privilege = 1
  6259.     where
  6260.         exists (
  6261.             select * from sysprotects
  6262.             where
  6263.                 id = @table_id
  6264.                 and (#table_priv1.uid = uid
  6265.                     or #table_priv1.gid = uid
  6266.                     or uid = 0)
  6267.                 and protecttype = 205
  6268.                 and action = 196)
  6269.         and not exists (select * from sysprotects
  6270.             where
  6271.                 id = @table_id
  6272.                 and (#table_priv1.uid = uid
  6273.                     or #table_priv1.gid = uid
  6274.                     or uid = 0)
  6275.                 and protecttype = 206
  6276.                 and action = 196)
  6277.  
  6278.     update #table_priv1
  6279.     set update_privilege = 1
  6280.     where
  6281.         exists (
  6282.             select * from sysprotects
  6283.             where
  6284.                 id = @table_id
  6285.                 and (#table_priv1.uid = uid
  6286.                     or #table_priv1.gid = uid
  6287.                     or uid = 0)
  6288.                 and protecttype = 205
  6289.                 and action = 197)
  6290.         and not exists (
  6291.             select * from sysprotects
  6292.             where
  6293.                 id = @table_id
  6294.                 and (#table_priv1.uid = uid
  6295.                     or #table_priv1.gid = uid
  6296.                     or uid = 0)
  6297.                 and protecttype = 206
  6298.                 and action = 197)
  6299.  
  6300.     update #table_priv1
  6301.     set references_privilege = 1
  6302.     where
  6303.         exists (
  6304.             select * from sysprotects
  6305.             where
  6306.                 id = @table_id
  6307.                 and (#table_priv1.uid = uid
  6308.                     or #table_priv1.gid = uid
  6309.                     or uid = 0)
  6310.                 and protecttype = 205
  6311.                 and action = 26)
  6312.         and not exists (
  6313.             select * from sysprotects
  6314.             where
  6315.                 id = @table_id
  6316.                 and (#table_priv1.uid = uid
  6317.                     or #table_priv1.gid = uid
  6318.                     or uid = 0)
  6319.                 and protecttype = 206
  6320.                 and action = 26)
  6321.  
  6322.     create table #table_priv2(
  6323.         table_qualifier varchar(32) NULL,
  6324.         table_owner     varchar(32) NULL,
  6325.         table_name        varchar(32) NOT NULL,
  6326.         grantor         varchar(32) NULL,
  6327.         grantee         varchar(32) NOT NULL,
  6328.         privilege        varchar(32) NOT NULL,
  6329.         is_grantable    varchar(3) NULL)
  6330.  
  6331.     insert into #table_priv2
  6332.         select
  6333.             table_qualifier,
  6334.             table_owner,
  6335.             table_name,
  6336.             grantor,
  6337.             grantee,
  6338.             'SELECT',
  6339.             is_grantable
  6340.         from #table_priv1
  6341.         where select_privilege = 1
  6342.  
  6343.  
  6344.     insert into #table_priv2
  6345.         select
  6346.             table_qualifier,
  6347.             table_owner,
  6348.             table_name,
  6349.             grantor,
  6350.             grantee,
  6351.             'INSERT',
  6352.             is_grantable
  6353.         from #table_priv1
  6354.         where insert_privilege = 1
  6355.  
  6356.  
  6357.     insert into #table_priv2
  6358.         select
  6359.             table_qualifier,
  6360.             table_owner,
  6361.             table_name,
  6362.             grantor,
  6363.             grantee,
  6364.             'DELETE',
  6365.             is_grantable
  6366.         from #table_priv1
  6367.         where delete_privilege = 1
  6368.  
  6369.  
  6370.     insert into #table_priv2
  6371.         select
  6372.             table_qualifier,
  6373.             table_owner,
  6374.             table_name,
  6375.             grantor,
  6376.             grantee,
  6377.             'UPDATE',
  6378.             is_grantable
  6379.         from #table_priv1
  6380.         where update_privilege = 1
  6381.  
  6382.     insert into #table_priv2
  6383.         select
  6384.             table_qualifier,
  6385.             table_owner,
  6386.             table_name,
  6387.             grantor,
  6388.             grantee,
  6389.             'REFERENCES',
  6390.             is_grantable
  6391.         from #table_priv1
  6392.         where references_privilege = 1
  6393.  
  6394.  
  6395.     select * from #table_priv2
  6396.     order by table_owner,table_name,privilege,grantee
  6397. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  6398. go
  6399.  
  6400. if (charindex('6.50', @@version) = 0 and
  6401.     charindex('7.00', @@version) = 0)
  6402. begin
  6403.     print ''
  6404.     print ''
  6405.     print 'Warning:'
  6406.     print 'you are installing the stored procedures '
  6407.     print 'on a pre 6.50 SQL Server.'
  6408.     print 'Ignore the following errors.'
  6409. end
  6410. else
  6411.     drop proc sp_table_privileges
  6412. go
  6413.  
  6414.  
  6415. /*    Procedure for 6.50 server */
  6416. CREATE PROCEDURE sp_table_privileges (
  6417.             @table_name         varchar(96),
  6418.             @table_owner        varchar(96) = null,
  6419.             @table_qualifier    varchar(32) = null)
  6420. as
  6421.  
  6422.     if @table_qualifier is not null
  6423.     begin
  6424.         if db_name() <> @table_qualifier
  6425.         begin    /* If qualifier doesn't match current database */
  6426.             raiserror (15250, -1,-1)
  6427.             return
  6428.         end
  6429.     end
  6430.     if @table_name is null
  6431.         select @table_name = '%'
  6432.     if @table_owner is null /* If no owner supplied, force wildcard */
  6433.         select @table_owner = '%'
  6434.  
  6435.     select
  6436.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  6437.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  6438.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  6439.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  6440.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  6441.         convert(varchar(32),case p.action
  6442.              when 193 then 'SELECT'
  6443.              when 195 then 'INSERT'
  6444.              when 196 then 'DELETE'
  6445.              when 197 then 'UPDATE'
  6446.              else 'REFERENCES'
  6447.         end) PRIVILEGE,
  6448.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6449.             else 'YES'
  6450.         end) IS_GRANTABLE
  6451.     from sysprotects p, sysobjects o, sysusers u
  6452.     where
  6453.         p.id = o.id
  6454.         and o.type in ('U','V','S')
  6455.         and object_name(o.id) like @table_name
  6456.         and user_name(o.uid) like @table_owner
  6457.             /* expand groups */
  6458.         and ((p.uid = u.uid and u.uid <> u.gid) or
  6459.              (p.uid = u.gid and u.uid <> u.gid))
  6460.         and p.protecttype <> 206    /* only grant rows */
  6461.         and p.action in (26,193,195,196,197)
  6462.         and o.uid <> u.uid            /* no rows for owner */
  6463.         and not exists (            /* exclude revoke'd privileges */
  6464.             select *
  6465.             from sysprotects p1
  6466.             where
  6467.                 p1.protecttype = 206
  6468.                 and p1.action = p.action
  6469.                 and p1.id = p.id
  6470.                 and p1.uid = u.uid)
  6471.     union all
  6472.     select    /*    Add rows for table owner */
  6473.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  6474.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  6475.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  6476.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  6477.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  6478.         convert(varchar(32),case v.number
  6479.             when 193 then 'SELECT'
  6480.             when 195 then 'INSERT'
  6481.             when 196 then 'DELETE'
  6482.             when 197 then 'UPDATE'
  6483.             else 'REFERENCES'
  6484.         end) PRIVILEGE,
  6485.         convert(varchar(3),'YES') IS_GRANTABLE
  6486.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6487.     where
  6488.         object_name(o.id) like @table_name
  6489.         and o.type in ('U','V','S')
  6490.         and user_name(o.uid) like @table_owner
  6491.         and u.suid = 1        /* grantor is dbo of database */
  6492.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6493.         and v.number in (26,193,195,196,197)
  6494.         and not exists (    /* exclude revoke'd privileges */
  6495.             select *
  6496.             from sysprotects p1
  6497.             where
  6498.                 p1.protecttype = 206
  6499.                 and p1.action = v.number
  6500.                 and p1.id = o.id
  6501.                 and p1.uid = o.uid)
  6502.     order by 2,3,6,5
  6503. go
  6504.  
  6505.  
  6506. if (charindex('7.00', @@version) > 0)
  6507.     drop proc sp_table_privileges
  6508. else
  6509. begin
  6510.     print ''
  6511.     print ''
  6512.     print 'Warning:'
  6513.     print 'you are installing the stored procedures '
  6514.     print 'on a pre 7.0 SQL Server.'
  6515.     print 'Ignore the following errors.'
  6516. end
  6517. go
  6518.  
  6519. /*    Procedure for 7.00 server */
  6520. CREATE PROCEDURE sp_table_privileges (
  6521.             @table_name         nvarchar(384),
  6522.             @table_owner        nvarchar(384) = null,
  6523.             @table_qualifier    sysname = null)
  6524. as
  6525.  
  6526.     if @table_qualifier is not null
  6527.     begin
  6528.         if db_name() <> @table_qualifier
  6529.         begin    /* If qualifier doesn't match current database */
  6530.             raiserror (15250, -1,-1)
  6531.             return
  6532.         end
  6533.     end
  6534.     if @table_name is null
  6535.         select @table_name = '%'
  6536.     if @table_owner is null /* If no owner supplied, force wildcard */
  6537.         select @table_owner = '%'
  6538.  
  6539.     select
  6540.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6541.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6542.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6543.         convert(sysname,user_name(p.grantor)) GRANTOR,
  6544.         convert(sysname,user_name(u.uid)) GRANTEE,
  6545.         convert(varchar(32),case p.action
  6546.              when 193 then 'SELECT'
  6547.              when 195 then 'INSERT'
  6548.              when 196 then 'DELETE'
  6549.              when 197 then 'UPDATE'
  6550.              else 'REFERENCES'
  6551.         end) PRIVILEGE,
  6552.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6553.             else 'YES'
  6554.         end) IS_GRANTABLE
  6555.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  6556.     where
  6557.         p.id = o.id
  6558.         and o.type in ('U','V','S')
  6559.         and object_name(o.id) like @table_name
  6560.         and user_name(o.uid) like @table_owner
  6561.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  6562.         and (u.uid > 0 and u.uid < 16384)
  6563.         and ((p.uid = u.uid) or
  6564.              (p.uid = m.groupuid and u.uid = m.memberuid))
  6565.         and p.protecttype <> 206    /* only grant rows */
  6566.         and p.action in (26,193,195,196,197)
  6567.         and o.uid <> u.uid            /* no rows for owner */
  6568.         and not exists (            /* exclude revoke'd privileges */
  6569.             select *
  6570.             from sysprotects p1
  6571.             where
  6572.                 p1.protecttype = 206
  6573.                 and p1.action = p.action
  6574.                 and p1.id = p.id
  6575.                 and p1.uid = u.uid)
  6576.     union all
  6577.     select    /*    Add rows for table owner */
  6578.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6579.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6580.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6581.         convert(sysname,user_name(u.uid)) GRANTOR,
  6582.         convert(sysname,user_name(o.uid)) GRANTEE,
  6583.         convert(varchar(32),case v.number
  6584.             when 193 then 'SELECT'
  6585.             when 195 then 'INSERT'
  6586.             when 196 then 'DELETE'
  6587.             when 197 then 'UPDATE'
  6588.             else 'REFERENCES'
  6589.         end) PRIVILEGE,
  6590.         convert(varchar(3),'YES') IS_GRANTABLE
  6591.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6592.     where
  6593.         object_name(o.id) like @table_name
  6594.         and o.type in ('U','V','S')
  6595.         and user_name(o.uid) like @table_owner
  6596.         and u.uid = 1        /* grantor is 'dbo' of database */
  6597.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6598.         and v.number in (26,193,195,196,197)
  6599.         and not exists (    /* exclude revoke'd privileges */
  6600.             select *
  6601.             from sysprotects p1
  6602.             where
  6603.                 p1.protecttype = 206
  6604.                 and p1.action = v.number
  6605.                 and p1.id = o.id
  6606.                 and p1.uid = o.uid)
  6607.     order by 2,3,6,5
  6608. go
  6609.  
  6610. grant execute on sp_table_privileges to public
  6611. go
  6612.  
  6613. dump tran master with no_log
  6614. go
  6615.  
  6616. print 'creating sp_tables'
  6617. go
  6618.  
  6619. /*    Procedure for 6.50 and earlier servers */
  6620. create procedure sp_tables(
  6621.                @table_name        varchar(96)    = null,
  6622.                @table_owner     varchar(96)    = null,
  6623.                @table_qualifier varchar(32)    = null,
  6624.                @table_type        varchar(100) = null)
  6625. as
  6626.     declare @type1 varchar(3)
  6627.     declare @tableindex int
  6628.  
  6629.  
  6630.     /* Special feature #1:    enumerate databases when owner and name
  6631.          are blank but qualifier is explicitly '%'.  */
  6632.     if @table_qualifier = '%' and
  6633.         @table_owner = '' and
  6634.         @table_name = ''
  6635.     begin    /* If enumerating databases */
  6636.         select
  6637.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  6638.             TABLE_OWNER = convert(varchar(32),null),
  6639.             TABLE_NAME = convert(varchar(32),null),
  6640.             TABLE_TYPE = convert(varchar(32),null),
  6641.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6642.         from master.dbo.sysdatabases d
  6643.         where d.name <> 'model'    /* eliminate MODEL database */
  6644.         order by 1
  6645.     end
  6646.  
  6647.     /* Special feature #2:    enumerate owners when qualifier and name
  6648.          are blank but owner is explicitly '%'.  */
  6649.     else if @table_qualifier = '' and
  6650.         @table_owner = '%' and
  6651.         @table_name = ''
  6652.     begin    /* If enumerating owners */
  6653.         select distinct
  6654.             TABLE_QUALIFIER = convert(varchar(32),null),
  6655.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  6656.             TABLE_NAME = convert(varchar(32),null),
  6657.             TABLE_TYPE = convert(varchar(32),null),
  6658.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6659.         from sysobjects
  6660.         order by 2
  6661.     end
  6662.  
  6663.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6664.          name are blank but table type is explicitly '%'.    */
  6665.     else if @table_qualifier = '' and
  6666.         @table_owner = '' and
  6667.         @table_name = '' and
  6668.         @table_type = '%'
  6669.     begin    /* If enumerating table types */
  6670.         select
  6671.             TABLE_QUALIFIER = convert(varchar(32),null),
  6672.             TABLE_OWNER = convert(varchar(32),null),
  6673.             TABLE_NAME = convert(varchar(32),null),
  6674.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6675.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6676.         from sysobjects o, syscolumns c
  6677.         where o.id=c.id and o.name='sysusers' and colid<=3
  6678.     end
  6679.  
  6680.     else
  6681.     begin /* end of special features - do normal processing */
  6682.         if @table_qualifier is not null
  6683.         begin
  6684.             if db_name() <> @table_qualifier
  6685.             begin
  6686.                 if @table_qualifier = ''
  6687.                 begin  /* If empty qualifier supplied */
  6688.                     /* Force an empty result set */
  6689.                     select @table_name = ''
  6690.                     select @table_owner = ''
  6691.                 end
  6692.                 else
  6693.                 begin    /* If qualifier doesn't match current database */
  6694.                     raiserror 20001 '~~Rush_5~~'
  6695.                     return
  6696.                 end
  6697.             end
  6698.         end
  6699.         if @table_type is null
  6700.         begin    /* Select all ODBC supported table types */
  6701.             select @type1 = 'SUV'
  6702.         end
  6703.         else
  6704.         begin
  6705.             /*    TableType is case sensitive if CS server */
  6706.             select @type1 = null
  6707.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6708.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6709.             if (charindex('''TABLE''',@table_type) <> 0)
  6710.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6711.             if (charindex('''VIEW''',@table_type) <> 0)
  6712.                 select @type1 = @type1 + 'V'    /* Add Views */
  6713.         end
  6714.         if @table_name is null
  6715.         begin    /*    If table name not supplied, match all */
  6716.             select @table_name = '%'
  6717.         end
  6718.         else
  6719.         begin
  6720.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6721.             begin    /* If owner not specified and table is specified */
  6722.                 if exists (select * from sysobjects
  6723.                     where uid = user_id()
  6724.                     and name = @table_name
  6725.                     and (type = 'U' or type = 'V' or type = 'S'))
  6726.                 begin    /* Override supplied owner w/owner of table */
  6727.                     select @table_owner = user_name()
  6728.                 end
  6729.             end
  6730.         end
  6731.         if @table_owner is null /* If no owner supplied, force wildcard */
  6732.             select @table_owner = '%'
  6733.         select
  6734.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6735.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6736.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6737.             TABLE_TYPE = convert(varchar(32),rtrim(
  6738.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6739.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6740.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6741.         from sysusers u, sysobjects o
  6742.         where
  6743.             o.name like @table_name
  6744.             and user_name(o.uid) like @table_owner
  6745.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6746.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6747.             and (
  6748.                 suser_id() = 1     /* User is the System Administrator */
  6749.                 or o.uid = user_id()     /* User created the object */
  6750.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6751.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6752.                     from sysprotects p
  6753.                     /* outer join to correlate with all rows in sysobjects */
  6754.                     where p.id =* o.id
  6755.                         /* get rows for public,current user,user's group */
  6756.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6757.                         /* check for SELECT,EXECUTE privilege */
  6758.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6759.                     ) = 1    /* final magic...compare Grants      */
  6760.             )
  6761.         order by 4, 1, 2, 3
  6762.     end
  6763. go
  6764.  
  6765. if (charindex('7.00', @@version) > 0)
  6766.     drop procedure sp_tables
  6767. else
  6768. begin
  6769.     print ''
  6770.     print ''
  6771.     print 'Warning:'
  6772.     print 'you are installing the stored procedures '
  6773.     print 'on a pre 7.0 SQL Server.'
  6774.     print 'Ignore the following errors.'
  6775. end
  6776. go
  6777.  
  6778. /*    Procedure for 7.00 server */
  6779. create procedure sp_tables(
  6780.                @table_name        nvarchar(384)    = null,
  6781.                @table_owner     nvarchar(384)    = null,
  6782.                @table_qualifier sysname    = null,
  6783.                @table_type        varchar(100) = null)
  6784. as
  6785.     declare @type1 varchar(3)
  6786.     declare @tableindex int
  6787.  
  6788.  
  6789.     /* Special feature #1:    enumerate databases when owner and name
  6790.          are blank but qualifier is explicitly '%'.  */
  6791.     if @table_qualifier = '%' and
  6792.         @table_owner = '' and
  6793.         @table_name = ''
  6794.     begin    /* If enumerating databases */
  6795.         select
  6796.             TABLE_QUALIFIER = convert(sysname,d.name),
  6797.             TABLE_OWNER = convert(sysname,null),
  6798.             TABLE_NAME = convert(sysname,null),
  6799.             TABLE_TYPE = convert(varchar(32),null),
  6800.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6801.         from master.dbo.sysdatabases d
  6802.         where d.name <> 'model'    /* eliminate MODEL database */
  6803.         order by 1
  6804.     end
  6805.  
  6806.     /* Special feature #2:    enumerate owners when qualifier and name
  6807.          are blank but owner is explicitly '%'.  */
  6808.     else if @table_qualifier = '' and
  6809.         @table_owner = '%' and
  6810.         @table_name = ''
  6811.     begin    /* If enumerating owners */
  6812.         select distinct
  6813.             TABLE_QUALIFIER = convert(sysname,null),
  6814.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6815.             TABLE_NAME = convert(sysname,null),
  6816.             TABLE_TYPE = convert(varchar(32),null),
  6817.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6818.         from sysobjects
  6819.         order by 2
  6820.     end
  6821.  
  6822.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6823.          name are blank but table type is explicitly '%'.    */
  6824.     else if @table_qualifier = '' and
  6825.         @table_owner = '' and
  6826.         @table_name = '' and
  6827.         @table_type = '%'
  6828.     begin    /* If enumerating table types */
  6829.         select
  6830.             TABLE_QUALIFIER = convert(sysname,null),
  6831.             TABLE_OWNER = convert(sysname,null),
  6832.             TABLE_NAME = convert(sysname,null),
  6833.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6834.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6835.         from sysobjects o, syscolumns c
  6836.         where o.id=c.id and o.name='sysusers' and colid<=3
  6837.     end
  6838.  
  6839.     else
  6840.     begin /* end of special features - do normal processing */
  6841.         if @table_qualifier is not null
  6842.         begin
  6843.             if db_name() <> @table_qualifier
  6844.             begin
  6845.                 if @table_qualifier = ''
  6846.                 begin  /* If empty qualifier supplied */
  6847.                     /* Force an empty result set */
  6848.                     select @table_name = ''
  6849.                     select @table_owner = ''
  6850.                 end
  6851.                 else
  6852.                 begin    /* If qualifier doesn't match current database */
  6853.                     raiserror (15250, -1,-1)
  6854.                     return
  6855.                 end
  6856.             end
  6857.         end
  6858.         if @table_type is null
  6859.         begin    /* Select all ODBC supported table types */
  6860.             select @type1 = 'SUV'
  6861.         end
  6862.         else
  6863.         begin
  6864.             /*    TableType is case sensitive if CS server */
  6865.             select @type1 = ''
  6866.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6867.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6868.             if (charindex('''TABLE''',@table_type) <> 0)
  6869.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6870.             if (charindex('''VIEW''',@table_type) <> 0)
  6871.                 select @type1 = @type1 + 'V'    /* Add Views */
  6872.         end
  6873.         if @table_name is null
  6874.         begin    /*    If table name not supplied, match all */
  6875.             select @table_name = '%'
  6876.         end
  6877.         else
  6878.         begin
  6879.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6880.             begin    /* If owner not specified and table is specified */
  6881.                 if exists (select * from sysobjects
  6882.                     where uid = user_id()
  6883.                     and name = @table_name
  6884.                     and (type = 'U' or type = 'V' or type = 'S'))
  6885.                 begin    /* Override supplied owner w/owner of table */
  6886.                     select @table_owner = user_name()
  6887.                 end
  6888.             end
  6889.         end
  6890.         if @table_owner is null /* If no owner supplied, force wildcard */
  6891.             select @table_owner = '%'
  6892.         select
  6893.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6894.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6895.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6896.             TABLE_TYPE = convert(varchar(32),rtrim(
  6897.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6898.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6899.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6900.         from sysobjects o
  6901.         where
  6902.             o.name like @table_name
  6903.             and user_name(o.uid) like @table_owner
  6904.             and o.type in ('U','V','S')
  6905.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6906.             and permissions (o.id)&4096 <> 0
  6907.         order by 4, 1, 2, 3
  6908.     end
  6909. go
  6910.  
  6911.  
  6912. grant execute on sp_tables to public
  6913. go
  6914.  
  6915. dump tran master with no_log
  6916. go
  6917.  
  6918. /*-----------------------------------------------------------------------------*/
  6919. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  6920. /*-----------------------------------------------------------------------------*/
  6921.  
  6922. if object_id('sp_linkedservers', 'P') is not null
  6923.     drop proc sp_linkedservers
  6924. go
  6925. raiserror(15339,-1,-1,'sp_linkedservers')
  6926. go
  6927. create proc sp_linkedservers as
  6928.     select
  6929.         SRV_NAME = srvname,
  6930.         SRV_PROVIDERNAME = providername,
  6931.         SRV_PRODUCT = srvproduct,
  6932.         SRV_DATASOURCE = datasource,
  6933.         SRV_PROVIDERSTRING = providerstring,
  6934.         SRV_LOCATION = location,
  6935.         SRV_CAT = catalog
  6936.     from master.dbo.sysservers
  6937.     order by 1
  6938. go
  6939. grant execute on sp_linkedservers to public
  6940. go
  6941.  
  6942.  
  6943. if object_id('sp_catalogs', 'P') is not null
  6944.     drop proc sp_catalogs
  6945. go
  6946. raiserror(15339,-1,-1,'sp_catalogs')
  6947. go
  6948. create procedure sp_catalogs(
  6949.     @server_name        sysname)
  6950. as
  6951.     select
  6952.         CATALOG_NAME,
  6953.         convert (nvarchar(255),DESCRIPTION)
  6954.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  6955.     order by CATALOG_NAME
  6956. go
  6957. grant execute on sp_catalogs to public
  6958. go
  6959.  
  6960.  
  6961. if object_id('sp_tables_ex', 'P') is not null
  6962.     drop proc sp_tables_ex
  6963. go
  6964. raiserror(15339,-1,-1,'sp_tables_ex')
  6965. go
  6966. create procedure sp_tables_ex(
  6967.     @table_server        sysname,
  6968.     @table_name            sysname = null,
  6969.     @table_schema        sysname = null,
  6970.     @table_catalog        sysname = null,
  6971.     @table_type            sysname = null)
  6972. as
  6973.     declare @table_catalog_param        sysname
  6974.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  6975.         (isnull(charindex('[', @table_catalog),0) = 0) and
  6976.         (isnull(charindex('_', @table_catalog),0) = 0))
  6977.         select @table_catalog_param = @table_catalog
  6978.     else
  6979.         select @table_catalog_param = null
  6980.  
  6981.     if ((isnull(charindex('%', @table_name),0) = 0) and
  6982.         (isnull(charindex('[', @table_name),0) = 0) and
  6983.         (isnull(charindex('_', @table_name),0) = 0))
  6984.     begin    /*    If no wild carding */
  6985.         select
  6986.             TABLE_CAT = TABLE_CATALOG,
  6987.             TABLE_SCHEM = TABLE_SCHEMA,
  6988.             TABLE_NAME = TABLE_NAME,
  6989.             TABLE_TYPE = TABLE_TYPE,
  6990.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  6991.         from master.dbo.SYSREMOTE_TABLES <
  6992.                     @table_server,
  6993.                     @table_catalog_param,
  6994.                     NULL,
  6995.                     @table_name,
  6996.                     NULL >
  6997.         where (TABLE_SCHEMA like @table_schema
  6998.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6999.             and (TABLE_CATALOG like @table_catalog
  7000.             or     @table_catalog is NULL)
  7001.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  7002.             or     @table_type is NULL)
  7003.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  7004.     end
  7005.     else
  7006.     begin    /*    If wild carding */
  7007.         select
  7008.             TABLE_CAT = TABLE_CATALOG,
  7009.             TABLE_SCHEM = TABLE_SCHEMA,
  7010.             TABLE_NAME = TABLE_NAME,
  7011.             TABLE_TYPE = TABLE_TYPE,
  7012.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  7013.         from master.dbo.SYSREMOTE_TABLES <
  7014.                     @table_server,
  7015.                     @table_catalog_param,
  7016.                     NULL,
  7017.                     NULL,
  7018.                     NULL >
  7019.         where (TABLE_SCHEMA like @table_schema
  7020.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7021.             and (TABLE_CATALOG like @table_catalog
  7022.             or     @table_catalog is NULL)
  7023.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  7024.             or     @table_type is NULL)
  7025.             and TABLE_NAME like @table_name
  7026.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  7027.     end
  7028. go
  7029. grant execute on sp_tables_ex to public
  7030. go
  7031.  
  7032.  
  7033. if object_id('sp_columns_ex', 'P') is not null
  7034.     drop proc sp_columns_ex
  7035. go
  7036. raiserror(15339,-1,-1,'sp_columns_ex')
  7037. go
  7038. create procedure sp_columns_ex(
  7039.     @table_server        sysname,
  7040.     @table_name            sysname = null,
  7041.     @table_schema        sysname = null,
  7042.     @table_catalog        sysname = null,
  7043.     @column_name        sysname = null,
  7044.     @ODBCVer            int = 2)
  7045. as
  7046.  
  7047.     set nocount on
  7048.     declare
  7049.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  7050.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  7051.  
  7052.     select
  7053.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  7054.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  7055.  
  7056.     declare    
  7057.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  7058.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  7059.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  7060.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  7061.     @DBTYPE_VARIANT smallint,        @DBTYPE_DECIMAL smallint,
  7062.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  7063.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  7064.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  7065.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  7066.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  7067.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  7068.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  7069.     @DBTYPE_DBTIMESTAMP smallint
  7070.  
  7071.     select
  7072.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  7073.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  7074.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  7075.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  7076.     @DBTYPE_VARIANT        = 12,    @DBTYPE_DECIMAL        = 14,
  7077.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  7078.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  7079.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  7080.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  7081.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  7082.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  7083.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  7084.     @DBTYPE_DBTIMESTAMP    = 135
  7085.  
  7086.     declare    
  7087.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  7088.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  7089.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  7090.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  7091.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  7092.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  7093.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  7094.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  7095.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  7096.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  7097.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  7098.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  7099.  
  7100.     select     
  7101.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  7102.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  7103.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  7104.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  7105.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  7106.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  7107.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  7108.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  7109.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  7110.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  7111.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  7112.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  7113.  
  7114.     declare 
  7115.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  7116.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  7117.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  7118.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  7119.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  7120.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  7121.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  7122.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  7123.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  7124.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  7125.     @ODBC_DATETIME smallint
  7126.  
  7127.     select
  7128.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  7129.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  7130.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  7131.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  7132.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  7133.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  7134.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  7135.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  7136.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  7137.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  7138.     @ODBC_DATETIME    =    
  7139.     case @ODBCVer
  7140.         when 2 then 11
  7141.         else 93
  7142.     end
  7143.  
  7144.     create table #tmp_columns
  7145.     (    TABLE_CAT sysname NULL,
  7146.         TABLE_SCHEM sysname NULL,
  7147.         TABLE_NAME sysname    NOT NULL,
  7148.         COLUMN_NAME sysname NULL,
  7149.         DATA_TYPE smallint NOT NULL,
  7150.         TYPE_NAME sysname  NULL,
  7151.         COLUMN_SIZE int NULL,
  7152.         BUFFER_LENGTH int NULL,
  7153.         DECIMAL_DIGITS smallint NULL,
  7154.         NUM_PREC_RADIX smallint NULL,
  7155.         NULLABLE smallint NOT NULL,
  7156.         REMARKS nvarchar(255) NULL,
  7157.         COLUMN_DEF nvarchar(127) NULL,
  7158.         SQL_DATA_TYPE smallint null,
  7159.         SQL_DATETIME_SUB smallint NULL,
  7160.         CHAR_OCTET_LENGTH int NULL,
  7161.         ORDINAL_POSITION smallint,
  7162.         IS_NULLABLE varchar(254) NOT NULL,
  7163.         SS_DATA_TYPE tinyint null,
  7164.         COLUMN_FLAGS int NOT NULL)
  7165.  
  7166.     if ((isnull(charindex('%', @table_name),0) = 0) and
  7167.         (isnull(charindex('[', @table_name),0) = 0) and
  7168.         (isnull(charindex('_', @table_name),0) = 0))
  7169.     begin    /*    If no wild carding */
  7170.         insert into #tmp_columns
  7171.         select
  7172.             TABLE_CAT = c.TABLE_CATALOG,
  7173.             TABLE_SCHEM = c.TABLE_SCHEMA,
  7174.             TABLE_NAME = c.TABLE_NAME,
  7175.             COLUMN_NAME = c.COLUMN_NAME,
  7176.             DATA_TYPE =
  7177.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  7178.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  7179.                 when @DBTYPE_I4                then @ODBC_INT
  7180.                 when @DBTYPE_R4                then @ODBC_REAL
  7181.                 when @DBTYPE_R8                then @ODBC_FLOAT
  7182.                 when @DBTYPE_CY                then @ODBC_MONEY
  7183.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  7184.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  7185.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  7186.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  7187.                 when @DBTYPE_BOOL            then @ODBC_BIT
  7188.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  7189.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  7190.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  7191.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  7192.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  7193.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  7194.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  7195.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  7196.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  7197.                 when @DBTYPE_GUID            then @ODBC_GUID
  7198.                 when @DBTYPE_BYTES then
  7199.                     case
  7200.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  7201.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7202.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7203.                                  p.COLUMN_SIZE = 8
  7204.                             then @ODBC_TIMESTAMP
  7205.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7206.                             then @ODBC_BINARY
  7207.                         else @ODBC_VARBINARY
  7208.                     end
  7209.                  when @DBTYPE_STR  then
  7210.                     case
  7211.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  7212.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  7213.                         else @ODBC_VARCHAR
  7214.                     end
  7215.                 when @DBTYPE_WSTR then
  7216.                     case
  7217.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7218.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7219.                         else @ODBC_NVARCHAR
  7220.                     end
  7221.                 when @DBTYPE_BSTR then
  7222.                     case
  7223.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7224.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7225.                         else @ODBC_NVARCHAR
  7226.                     end
  7227.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  7228.             end,
  7229.             TYPE_NAME = p.TYPE_NAME,
  7230.             COLUMN_SIZE =
  7231.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  7232.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7233.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  7234.             NUM_PREC_RADIX = NULL,
  7235.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  7236.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  7237.             COLUMN_DEF = c.COLUMN_DEFAULT,
  7238.             SQL_DATA_TYPE = NULL,
  7239.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  7240.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7241.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  7242.             IS_NULLABLE =
  7243.             case c.IS_NULLABLE
  7244.                 when 1 then 'YES'
  7245.                 else 'NO'
  7246.             end,
  7247.             SS_DATA_TYPE = NULL,
  7248.             COLUMN_FLAGS = c.COLUMN_FLAGS
  7249.         from master.dbo.SYSREMOTE_COLUMNS <
  7250.                     @table_server,
  7251.                     @table_catalog,
  7252.                     NULL,
  7253.                     @table_name,
  7254.                     NULL > c
  7255.             LEFT OUTER JOIN
  7256.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  7257.                     @table_server > p
  7258.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  7259.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  7260.             where (TABLE_SCHEMA like @table_schema
  7261.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7262.                 and (c.COLUMN_NAME like @column_name
  7263.                 or     @column_name is NULL)
  7264.     end
  7265.     else
  7266.     begin    /*    If wild carding */
  7267.         insert into #tmp_columns
  7268.         select
  7269.             TABLE_CAT = c.TABLE_CATALOG,
  7270.             TABLE_SCHEM = c.TABLE_SCHEMA,
  7271.             TABLE_NAME = c.TABLE_NAME,
  7272.             COLUMN_NAME = c.COLUMN_NAME,
  7273.             DATA_TYPE =
  7274.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  7275.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  7276.                 when @DBTYPE_I4                then @ODBC_INT
  7277.                 when @DBTYPE_R4                then @ODBC_REAL
  7278.                 when @DBTYPE_R8                then @ODBC_FLOAT
  7279.                 when @DBTYPE_CY                then @ODBC_MONEY
  7280.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  7281.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  7282.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  7283.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  7284.                 when @DBTYPE_BOOL            then @ODBC_BIT
  7285.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  7286.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  7287.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  7288.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  7289.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  7290.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  7291.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  7292.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  7293.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  7294.                 when @DBTYPE_GUID            then @ODBC_GUID
  7295.                 when @DBTYPE_BYTES then
  7296.                     case
  7297.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  7298.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7299.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7300.                                  p.COLUMN_SIZE = 8
  7301.                             then @ODBC_TIMESTAMP
  7302.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7303.                             then @ODBC_BINARY
  7304.                         else @ODBC_VARBINARY
  7305.                     end
  7306.                  when @DBTYPE_STR  then
  7307.                     case
  7308.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  7309.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  7310.                         else @ODBC_VARCHAR
  7311.                     end
  7312.                 when @DBTYPE_WSTR then
  7313.                     case
  7314.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7315.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7316.                         else @ODBC_NVARCHAR
  7317.                     end
  7318.                 when @DBTYPE_BSTR then
  7319.                     case
  7320.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  7321.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  7322.                         else @ODBC_NVARCHAR
  7323.                     end
  7324.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  7325.             end,
  7326.             TYPE_NAME = p.TYPE_NAME,
  7327.             COLUMN_SIZE =
  7328.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  7329.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7330.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  7331.             NUM_PREC_RADIX = NULL,
  7332.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  7333.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  7334.             COLUMN_DEF = c.COLUMN_DEFAULT,
  7335.             SQL_DATA_TYPE = NULL,
  7336.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  7337.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  7338.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  7339.             IS_NULLABLE =
  7340.             case c.IS_NULLABLE
  7341.                 when 1 then 'YES'
  7342.                 else 'NO'
  7343.             end,
  7344.             SS_DATA_TYPE = NULL,
  7345.             COLUMN_FLAGS = c.COLUMN_FLAGS
  7346.         from master.dbo.SYSREMOTE_COLUMNS <
  7347.                     @table_server,
  7348.                     @table_catalog,
  7349.                     NULL,
  7350.                     NULL,
  7351.                     NULL > c
  7352.             LEFT OUTER JOIN
  7353.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  7354.                     @table_server > p
  7355.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  7356.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  7357.             where (TABLE_SCHEMA like @table_schema
  7358.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7359.                 and (c.COLUMN_NAME like @column_name
  7360.                 or     @column_name is NULL)
  7361.                 and c.TABLE_NAME like @table_name
  7362.     end
  7363.  
  7364.     update #tmp_columns
  7365.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  7366.         SQL_DATETIME_SUB =
  7367.         case
  7368.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  7369.             else #tmp_columns.SQL_DATETIME_SUB
  7370.         end,
  7371.         TYPE_NAME = 
  7372.         case 
  7373.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7374.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7375.                     BUFFER_LENGTH = 8
  7376.                 then 'timestamp'
  7377.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  7378.                     #tmp_columns.SQL_DATETIME_SUB = 0
  7379.                 then 'smalldatetime'
  7380.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  7381.                     #tmp_columns.COLUMN_SIZE = 10
  7382.                 then 'smallmoney'
  7383.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  7384.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  7385.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  7386.                 then spt_dt.TYPE_NAME
  7387.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  7388.         end,
  7389.         COLUMN_SIZE =
  7390.         case
  7391.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  7392.                 spt_dt.data_precision
  7393.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  7394.                     #tmp_columns.SQL_DATETIME_SUB = 0
  7395.                 then 16
  7396.             else #tmp_columns.COLUMN_SIZE
  7397.         end,
  7398.         BUFFER_LENGTH =
  7399.         case
  7400.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  7401.                 COLUMN_SIZE+2
  7402.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  7403.             else #tmp_columns.BUFFER_LENGTH
  7404.         end,
  7405.         DECIMAL_DIGITS =
  7406.         case
  7407.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  7408.                 then #tmp_columns.SQL_DATETIME_SUB
  7409.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  7410.         end,
  7411.         NUM_PREC_RADIX = spt_dt.RADIX,
  7412.         SS_DATA_TYPE = 
  7413.         case
  7414.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  7415.                 and #tmp_columns.NULLABLE = 1
  7416.                     then @ODS_INT_NULL
  7417.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  7418.                 then @ODS_TINYINT_NOT_NULL
  7419.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  7420.                 then @ODS_SMALLINT_NOT_NULL
  7421.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  7422.                 then @ODS_INT_NOT_NULL
  7423.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  7424.                 then @ODS_FLOAT_NULL
  7425.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  7426.                 then @ODS_REAL_NOT_NULL
  7427.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  7428.                 then @ODS_FLOAT_NOT_NULL
  7429.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  7430.                 then @ODS_MONEY_NULL
  7431.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  7432.                 then @ODS_MONEY_NOT_NULL
  7433.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  7434.                 then @ODS_DATETIME_NULL
  7435.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  7436.                 then @ODS_DATETIME_NOT_NULL
  7437.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  7438.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  7439.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  7440.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  7441.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  7442.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  7443.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  7444.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  7445.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  7446.                     BUFFER_LENGTH = 8
  7447.                 then @ODS_TIMESTAMP
  7448.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  7449.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7450.                 then  @ODS_BINARY_NULL
  7451.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  7452.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  7453.                 then @ODS_BINARY_NOT_NULL
  7454.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  7455.                 then @ODS_VARBINARY    
  7456.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  7457.                 then @ODS_CHAR_NULL
  7458.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  7459.                 then @ODS_CHAR_NOT_NULL
  7460.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  7461.             else null
  7462.         end
  7463.         from master.dbo.spt_datatype_info spt_dt
  7464.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  7465.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  7466.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  7467.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  7468.  
  7469.     select
  7470.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  7471.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  7472.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  7473.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  7474.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  7475.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  7476.             SS_DATA_TYPE
  7477.         from #tmp_columns
  7478.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  7479.  
  7480.         drop table #tmp_columns
  7481.  
  7482. go
  7483. grant execute on sp_columns_ex to public
  7484. go
  7485.  
  7486.  
  7487. if object_id('sp_table_privileges_ex', 'P') is not null
  7488.     drop proc sp_table_privileges_ex
  7489. go
  7490. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  7491. go
  7492. create procedure sp_table_privileges_ex(
  7493.     @table_server        sysname,
  7494.     @table_name            sysname = null,
  7495.     @table_schema        sysname = null,
  7496.     @table_catalog        sysname = null)
  7497. as
  7498.     select
  7499.         TABLE_CAT = TABLE_CATALOG,
  7500.         TABLE_SCHEM = TABLE_SCHEMA,
  7501.         TABLE_NAME = TABLE_NAME,
  7502.         GRANTOR = GRANTOR,
  7503.         GRANTEE = GRANTEE,
  7504.         PRIVILEGE = PRIVILEGE_TYPE,
  7505.         IS_GRANTABLE = 
  7506.         case IS_GRANTABLE
  7507.             when 1 then 'YES'
  7508.             when 0 then 'NO'
  7509.             else null
  7510.         end
  7511.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  7512.                 @table_server,
  7513.                 @table_catalog,
  7514.                 NULL,
  7515.                 NULL >
  7516.     where (TABLE_SCHEMA like @table_schema
  7517.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7518.         and (TABLE_NAME like @table_name
  7519.         or     @table_name is NULL)
  7520.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  7521. go
  7522. grant execute on sp_table_privileges_ex to public
  7523. go
  7524.  
  7525.  
  7526. if object_id('sp_column_privileges_ex', 'P') is not null
  7527.     drop proc sp_column_privileges_ex
  7528. go
  7529. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  7530. go
  7531. create procedure sp_column_privileges_ex(
  7532.     @table_server        sysname,
  7533.     @table_name            sysname = null,
  7534.     @table_schema        sysname = null,
  7535.     @table_catalog        sysname = null,
  7536.     @column_name        sysname = null)
  7537. as
  7538.     select
  7539.         TABLE_CAT = TABLE_CATALOG,
  7540.         TABLE_SCHEM = TABLE_SCHEMA,
  7541.         TABLE_NAME = TABLE_NAME,
  7542.         COLUMN_NAME = COLUMN_NAME,
  7543.         GRANTOR = GRANTOR,
  7544.         GRANTEE = GRANTEE,
  7545.         PRIVILEGE = PRIVILEGE_TYPE,
  7546.         IS_GRANTABLE = 
  7547.         case IS_GRANTABLE
  7548.             when 1 then 'YES'
  7549.             when 0 then 'NO'
  7550.             else null
  7551.         end
  7552.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  7553.         @table_server,
  7554.         @table_catalog,
  7555.         @table_schema,
  7556.         @table_name,
  7557.         NULL >
  7558.     where (COLUMN_NAME like @column_name
  7559.         or @column_name is NULL)
  7560.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  7561. go
  7562. grant execute on sp_column_privileges_ex to public
  7563. go
  7564.  
  7565.  
  7566. if object_id('sp_indexes', 'P') is not null
  7567.     drop proc sp_indexes
  7568. go
  7569. raiserror(15339,-1,-1,'sp_indexes')
  7570. go
  7571. create procedure sp_indexes(
  7572.     @table_server        sysname,
  7573.     @table_name            sysname = null,
  7574.     @table_schema        sysname = null,
  7575.     @table_catalog        sysname = null,
  7576.     @index_name            sysname = null,
  7577.     @is_unique          bit = null)
  7578. as
  7579.     select
  7580.         TABLE_CAT = TABLE_CATALOG,
  7581.         TABLE_SCHEM = TABLE_SCHEMA,
  7582.         TABLE_NAME = TABLE_NAME,
  7583.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  7584.         INDEX_QUALIFIER = TABLE_NAME,
  7585.         INDEX_NAME = INDEX_NAME,
  7586.         TYPE = 
  7587.         case [CLUSTERED]
  7588.             when 1 then 1
  7589.             else 3
  7590.         end,
  7591.         ORDINAL_POSITION = ORDINAL_POSITION,
  7592.         COLUMN_NAME = COLUMN_NAME,
  7593.         ASC_OR_DESC = 
  7594.         case [COLLATION]
  7595.             when 1 then 'A'
  7596.             when 2 then 'D'
  7597.             else null
  7598.         end,
  7599.         CARDINALITY = CARDINALITY,
  7600.         PAGES = 
  7601.         case [CLUSTERED]
  7602.             when 1 then PAGES
  7603.             else NULL
  7604.         end,
  7605.         FILTER_CONDITION = FILTER_CONDITION
  7606.     from master.dbo.SYSREMOTE_INDEXES <
  7607.                 @table_server,
  7608.                 @table_catalog,
  7609.                 @table_schema,
  7610.                 @index_name,
  7611.                 NULL,            /* TYPE (index type) */
  7612.                 @table_name >
  7613.     where @is_unique is null or @is_unique = [UNIQUE]
  7614.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  7615. go
  7616. grant execute on sp_indexes to public
  7617. go
  7618.  
  7619.  
  7620. if object_id('sp_foreignkeys', 'P') is not null
  7621.     drop proc sp_foreignkeys
  7622. go
  7623. raiserror(15339,-1,-1,'sp_foreignkeys')
  7624. go
  7625. create procedure sp_foreignkeys(
  7626.     @table_server        sysname,
  7627.     @pktab_name         sysname = null,
  7628.     @pktab_schema       sysname = null,
  7629.     @pktab_catalog      sysname = null,
  7630.     @fktab_name         sysname = null,
  7631.     @fktab_schema       sysname = null,
  7632.     @fktab_catalog      sysname = null)
  7633. as
  7634.     select
  7635.         PKTABLE_CAT = PK_TABLE_CATALOG,
  7636.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  7637.         PKTABLE_NAME = PK_TABLE_NAME,
  7638.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  7639.         FKTABLE_CAT = FK_TABLE_CATALOG,
  7640.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  7641.         FKTABLE_NAME = FK_TABLE_NAME,
  7642.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  7643.         KEY_SEQ = ORDINAL,
  7644.         UPDATE_RULE = 
  7645.         case UPDATE_RULE
  7646.             when 'CASCADE' then 0
  7647.             when 'NO ACTION' then 1
  7648.             when 'SET NULL' then 2
  7649.             else null
  7650.         end,
  7651.         DELETE_RULE = 
  7652.         case DELETE_RULE
  7653.             when 'CASCADE' then 0
  7654.             when 'NO ACTION' then 1
  7655.             when 'SET NULL' then 2
  7656.             else null
  7657.         end,
  7658.         FK_NAME = convert(sysname, NULL),
  7659.         PK_NAME = convert(sysname, NULL),
  7660.         DEFERRABILITY = convert(smallint, null)
  7661.  
  7662.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  7663.                 @table_server,
  7664.                 @pktab_catalog,
  7665.                 @pktab_schema,
  7666.                 @pktab_name,
  7667.                 @fktab_catalog,
  7668.                 @fktab_schema,
  7669.                 @fktab_name >
  7670.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  7671. go
  7672. grant execute on sp_foreignkeys to public
  7673. go
  7674.  
  7675.  
  7676. if object_id('sp_primarykeys', 'P') is not null
  7677.     drop proc sp_primarykeys
  7678. go
  7679. raiserror(15339,-1,-1,'sp_primarykeys')
  7680. go
  7681. create procedure sp_primarykeys(
  7682.     @table_server        sysname,
  7683.     @table_name         sysname = null,
  7684.     @table_schema       sysname = null,
  7685.     @table_catalog      sysname = null)
  7686. as
  7687.     select
  7688.         TABLE_CAT = TABLE_CATALOG,
  7689.         TABLE_SCHEM = TABLE_SCHEMA,
  7690.         TABLE_NAME = TABLE_NAME,
  7691.         COLUMN_NAME = COLUMN_NAME,
  7692.         KEY_SEQ = ORDINAL,
  7693.         PK_NAME = convert(sysname, NULL)
  7694.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  7695.                 @table_server,
  7696.                 @table_catalog,
  7697.                 @table_schema,
  7698.                 @table_name >
  7699.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  7700. go
  7701. grant execute on sp_primarykeys to public
  7702. go
  7703.  
  7704. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  7705.  
  7706.  
  7707. dump tran master with no_log
  7708. go
  7709.  
  7710. if (charindex('6.50', @@version) = 0 and
  7711.     charindex('7.00', @@version) = 0)
  7712. begin
  7713.     print ''
  7714.     print ''
  7715.     print 'Warning:'
  7716.     print 'you are installing the stored procedures '
  7717.     print 'on a pre 6.50 SQL Server.'
  7718.     print 'Ignore the following errors.'
  7719. end
  7720.  
  7721. print 'creating sp_ddopen'
  7722. go
  7723.  
  7724. /*    Procedure for pre-7.00 server */
  7725. create procedure sp_ddopen(
  7726.                @handle            int output,
  7727.                @procname        sysname,
  7728.                @scrollopt        int output,
  7729.                @ccopt            int output,
  7730.                @rows            int output,
  7731.                @p1                varchar(255) = null,
  7732.                @p2                varchar(255) = null,
  7733.                @p3                varchar(255) = null,
  7734.                @p4                varchar(255) = null,
  7735.                @p5                varchar(255) = null,
  7736.                @p6                varchar(255) = null,
  7737.                @p7                int = null,
  7738.                @ODBCVer         int = 2)
  7739. as
  7740.     set nocount on
  7741.     declare @ret int
  7742.  
  7743.     if @procname = 'sp_column_privileges'
  7744.     begin
  7745.         create table #spcolpriv (
  7746.             TABLE_QUALIFIER varchar(32) null,
  7747.             TABLE_OWNER varchar(32) null,
  7748.             TABLE_NAME varchar(32)    not null,
  7749.             COLUMN_NAME varchar(32) not null,
  7750.             GRANTOR varchar(32) null,
  7751.             GRANTEE varchar(32) not null,
  7752.             PRIVILEGE varchar(32) not null,
  7753.             IS_GRANTABLE varchar(3) null
  7754.             )
  7755.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7756.         exec @ret = sp_cursoropen @handle output,
  7757.             'select * from #spcolpriv',
  7758.             @scrollopt output, @ccopt output, @rows output
  7759.         drop table #spcolpriv
  7760.     end
  7761.     else if @procname = 'sp_columns'
  7762.     begin
  7763.         create table #spcolumns (
  7764.             TABLE_QUALIFIER varchar(32) null,
  7765.             TABLE_OWNER varchar(32) null,
  7766.             TABLE_NAME varchar(32)    not null,
  7767.             COLUMN_NAME varchar(32) not null,
  7768.             DATA_TYPE smallint not null,
  7769.             TYPE_NAME varchar(32) not null,
  7770.             "PRECISION" int null,
  7771.             LENGTH int null,
  7772.             SCALE smallint null,
  7773.             RADIX smallint null,
  7774.             NULLABLE smallint not null,
  7775.             REMARKS varchar(254) null,
  7776.             COLUMN_DEF varchar(255) null,
  7777.             SQL_DATA_TYPE smallint not null,
  7778.             SQL_DATETIME_SUB smallint null,
  7779.             CHAR_OCTET_LENGTH int null,
  7780.             ORDINAL_POSITION int not null,
  7781.             IS_NULLABLE varchar(254) null,
  7782.             SS_DATA_TYPE tinyint null
  7783.             )
  7784.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7785.         exec @ret = sp_cursoropen @handle output,
  7786.             'select * from #spcolumns',
  7787.             @scrollopt output, @ccopt output, @rows output
  7788.         drop table #spcolumns
  7789.     end
  7790.     else if @procname = 'sp_datatype_info'
  7791.     begin
  7792.         create table #spdatatypeinfo (
  7793.             TYPE_NAME            varchar(32)  not null,
  7794.             DATA_TYPE            smallint not null,
  7795.             "PRECISION"            int null,
  7796.             LITERAL_PREFIX        varchar(32)    null,
  7797.             LITERAL_SUFFIX        varchar(32)    null,
  7798.             CREATE_PARAMS        varchar(32)    null,
  7799.             NULLABLE            smallint   not null,
  7800.             CASE_SENSITIVE        smallint   not null,
  7801.             SEARCHABLE            smallint   not null,
  7802.             UNSIGNED_ATTRIBUTE    smallint   null,
  7803.             MONEY    smallint    not null,
  7804.             AUTO_INCREMENT        smallint    null,
  7805.             LOCAL_TYPE_NAME     varchar(32) null,
  7806.             MINIMUM_SCALE        smallint     null,
  7807.             MAXIMUM_SCALE        smallint   null,
  7808.             SQL_DATA_TYPE        smallint      not null,
  7809.             SQL_DATETIME_SUB    smallint   null,
  7810.             NUM_PREC_RADIX        int     null,
  7811.             INTERVAL_PRECISION    smallint    NULL,
  7812.             USERTYPE            smallint not null)
  7813.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7814.         exec @ret = sp_cursoropen @handle output,
  7815.             'select * from #spdatatypeinfo',
  7816.             @scrollopt output, @ccopt output, @rows output
  7817.         drop table #spdatatypeinfo
  7818.     end
  7819.     else if @procname = 'sp_fkeys'
  7820.     begin
  7821.         create table #spfkeys (
  7822.             PKTABLE_QUALIFIER varchar(32)     null,
  7823.             PKTABLE_OWNER varchar(32)    null,
  7824.             PKTABLE_NAME varchar(32)  not null,
  7825.             PKCOLUMN_NAME varchar(32)    not null,
  7826.             FKTABLE_QUALIFIER varchar(32)    null,
  7827.             FKTABLE_OWNER varchar(32)    null,
  7828.             FKTABLE_NAME varchar(32)  not null,
  7829.             FKCOLUMN_NAME varchar(32)    not null,
  7830.             KEY_SEQ smallint not null,
  7831.             UPDATE_RULE smallint null,
  7832.             DELETE_RULE smallint null,
  7833.             FK_NAME varchar(32) null,
  7834.             PK_NAME varchar(32) null,
  7835.             DEFERRABILITY smallint null
  7836.             )
  7837.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7838.         exec @ret = sp_cursoropen @handle output,
  7839.             'select * from #spfkeys',
  7840.             @scrollopt output, @ccopt output, @rows output
  7841.         drop table #spfkeys
  7842.     end
  7843.     else if @procname = 'sp_pkeys'
  7844.     begin
  7845.         create table #sppkeys (
  7846.             TABLE_QUALIFIER varchar(32)   null,
  7847.             TABLE_OWNER varchar(32)   null,
  7848.             TABLE_NAME varchar(32)    not null,
  7849.             COLUMN_NAME varchar(32)  not null,
  7850.             KEY_SEQ smallint not null,
  7851.             PK_NAME varchar(32) null
  7852.             )
  7853.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7854.         exec @ret = sp_cursoropen @handle output,
  7855.             'select * from #sppkeys',
  7856.             @scrollopt output, @ccopt output, @rows output
  7857.         drop table #sppkeys
  7858.     end
  7859.     else if @procname = 'sp_special_columns'
  7860.     begin
  7861.         create table #spspeccol (
  7862.             SCOPE smallint null,
  7863.             COLUMN_NAME varchar(32) not null,
  7864.             DATA_TYPE smallint not null,
  7865.             TYPE_NAME varchar(32) not null,
  7866.             "PRECISION" int null,
  7867.             LENGTH int null,
  7868.             SCALE smallint null,
  7869.             PSEUDO_COLUMN smallint null
  7870.             )
  7871.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7872.         exec @ret = sp_cursoropen @handle output,
  7873.             'select * from #spspeccol',
  7874.             @scrollopt output, @ccopt output, @rows output
  7875.         drop table #spspeccol
  7876.     end
  7877.     else if @procname = 'sp_sproc_columns'
  7878.     begin
  7879.         create table #spproccol (
  7880.             PROCEDURE_QUALIFIER varchar(32)  null,
  7881.             PROCEDURE_OWNER varchar(32)  null,
  7882.             PROCEDURE_NAME varchar(32)    not null,
  7883.             COLUMN_NAME varchar(32) not null,
  7884.             COLUMN_TYPE smallint not null,
  7885.             DATA_TYPE smallint not null,
  7886.             TYPE_NAME varchar(32) not null,
  7887.             "PRECISION" int null,
  7888.             LENGTH int null,
  7889.             SCALE smallint null,
  7890.             RADIX smallint null,
  7891.             NULLABLE smallint not null,
  7892.             REMARKS varchar(254) null,
  7893.             COLUMN_DEF varchar(255) null,
  7894.             SQL_DATA_TYPE smallint not null,
  7895.             SQL_DATETIME_SUB smallint null,
  7896.             CHAR_OCTET_LENGTH int null,
  7897.             ORDINAL_POSITION int not null,
  7898.             IS_NULLABLE varchar(254) null,
  7899.             SS_DATA_TYPE tinyint null
  7900.             )
  7901.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  7902.         exec @ret = sp_cursoropen @handle output,
  7903.             'select * from #spproccol',
  7904.             @scrollopt output, @ccopt output, @rows output
  7905.         drop table #spproccol
  7906.     end
  7907.     else if @procname = 'sp_statistics'
  7908.     begin
  7909.         create table #spstatistics (
  7910.             TABLE_QUALIFIER varchar(32)   null,
  7911.             TABLE_OWNER varchar(32)   null,
  7912.             TABLE_NAME varchar(32)    not null,
  7913.             NON_UNIQUE smallint null,
  7914.             INDEX_QUALIFIER varchar(32) null,
  7915.             INDEX_NAME varchar(32)    null,
  7916.             TYPE smallint not null,
  7917.             SEQ_IN_INDEX smallint null,
  7918.             COLUMN_NAME varchar(32) null,
  7919.             COLLATION char(1) null,
  7920.             CARDINALITY int null,
  7921.             PAGES int null,
  7922.             FILTER_CONDITION varchar(128) null
  7923.             )
  7924.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  7925.         exec @ret = sp_cursoropen @handle output,
  7926.             'select * from #spstatistics',
  7927.             @scrollopt output, @ccopt output, @rows output
  7928.         drop table #spstatistics
  7929.     end
  7930.     else if @procname = 'sp_stored_procedures'
  7931.     begin
  7932.         create table #spprocedures (
  7933.             PROCEDURE_QUALIFIER varchar(32)  null,
  7934.             PROCEDURE_OWNER varchar(32)  null,
  7935.             PROCEDURE_NAME varchar(32)    not null,
  7936.             NUM_INPUT_PARAMS int null,
  7937.             NUM_OUTPUT_PARAMS int null,
  7938.             NUM_RESULT_SETS int null,
  7939.             REMARKS varchar(254) null,
  7940.             PROCEDURE_TYPE smallint null
  7941.             )
  7942.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  7943.         exec @ret = sp_cursoropen @handle output,
  7944.             'select * from #spprocedures',
  7945.             @scrollopt output, @ccopt output, @rows output
  7946.         drop table #spprocedures
  7947.     end
  7948.     else if @procname = 'sp_table_privileges'
  7949.     begin
  7950.         create table #sptabpriv (
  7951.             TABLE_QUALIFIER varchar(32) null,
  7952.             TABLE_OWNER varchar(32) null,
  7953.             TABLE_NAME varchar(32)    not null,
  7954.             GRANTOR varchar(32) null,
  7955.             GRANTEE varchar(32) not null,
  7956.             PRIVILEGE varchar(32) not null,
  7957.             IS_GRANTABLE varchar(3) null
  7958.             )
  7959.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  7960.         exec @ret = sp_cursoropen @handle output,
  7961.             'select * from #sptabpriv',
  7962.             @scrollopt output, @ccopt output, @rows output
  7963.         drop table #sptabpriv
  7964.     end
  7965.     else if @procname = 'sp_tables'
  7966.     begin
  7967.         create table #sptables (
  7968.             TABLE_QUALIFIER varchar(32) null,
  7969.             TABLE_OWNER varchar(32) null,
  7970.             TABLE_NAME varchar(32)    null,
  7971.             TABLE_TYPE     varchar(32) null,
  7972.             REMARKS varchar(254) null)
  7973.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  7974.         exec @ret = sp_cursoropen @handle output,
  7975.             'select * from #sptables',
  7976.             @scrollopt output, @ccopt output, @rows output
  7977.         drop table #sptables
  7978.     end
  7979.     select @ret = isnull(@ret,0)
  7980.     return isnull(@ret,0)
  7981. go
  7982.  
  7983. if (charindex('7.00', @@version) > 0)
  7984.     drop procedure sp_ddopen
  7985. else
  7986. begin
  7987.     print ''
  7988.     print ''
  7989.     print 'Warning:'
  7990.     print 'you are installing the stored procedures '
  7991.     print 'on a pre 7.0 SQL Server.'
  7992.     print 'Ignore the following errors.'
  7993. end
  7994. go
  7995.  
  7996. /*    Procedure for 7.00 server */
  7997. create procedure sp_ddopen; 1(
  7998.                @handle            int output,
  7999.                @procname        sysname,
  8000.                @scrollopt        int output,
  8001.                @ccopt            int output,
  8002.                @rows            int output,
  8003.                @p1                nvarchar(774) = null,
  8004.                @p2                nvarchar(774) = null,
  8005.                @p3                nvarchar(774) = null,
  8006.                @p4                nvarchar(774) = null,
  8007.                @p5                nvarchar(774) = null,
  8008.                @p6                nvarchar(774) = null,
  8009.                @p7                int = null,
  8010.                @ODBCVer         int = 2)
  8011. as
  8012.     set nocount on
  8013.     declare @ret int
  8014.  
  8015.     if @procname = 'sp_column_privileges'
  8016.     begin
  8017.         exec @ret = sp_ddopen;2 @handle output, 
  8018.                             @scrollopt output, 
  8019.                             @ccopt output, 
  8020.                             @rows output, 
  8021.                             @p1,
  8022.                             @p2,
  8023.                             @p3,
  8024.                             @p4
  8025.     end
  8026.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  8027.     begin
  8028.         exec @ret = sp_ddopen;3 @handle output,
  8029.                             @procname,
  8030.                             @scrollopt output, 
  8031.                             @ccopt output, 
  8032.                             @rows output, 
  8033.                             @p1,
  8034.                             @p2,
  8035.                             @p3,
  8036.                             @p4,
  8037.                             @p5,
  8038.                             @ODBCVer
  8039.     end
  8040.     else if @procname = 'sp_datatype_info'
  8041.     begin
  8042.         exec @ret = sp_ddopen;4 @handle output,
  8043.                             @scrollopt output, 
  8044.                             @ccopt output, 
  8045.                             @rows output, 
  8046.                             @p7,
  8047.                             @ODBCVer
  8048.     end
  8049.     else if @procname = 'sp_fkeys'
  8050.     begin
  8051.         exec @ret = sp_ddopen;5 @handle output, 
  8052.                             @scrollopt output, 
  8053.                             @ccopt output, 
  8054.                             @rows output, 
  8055.                             @p1,
  8056.                             @p2,
  8057.                             @p3,
  8058.                             @p4,
  8059.                             @p5,
  8060.                             @p6
  8061.     end
  8062.     else if @procname = 'sp_pkeys'
  8063.     begin
  8064.         exec @ret = sp_ddopen;6 @handle output, 
  8065.                             @scrollopt output, 
  8066.                             @ccopt output, 
  8067.                             @rows output, 
  8068.                             @p1,
  8069.                             @p2,
  8070.                             @p3
  8071.     end
  8072.     else if @procname = 'sp_special_columns'
  8073.     begin
  8074.         exec @ret = sp_ddopen;7 @handle output, 
  8075.                             @scrollopt output, 
  8076.                             @ccopt output, 
  8077.                             @rows output, 
  8078.                             @p1,
  8079.                             @p2,
  8080.                             @p3,
  8081.                             @p4,
  8082.                             @p5,
  8083.                             @p6,
  8084.                             @ODBCVer
  8085.     end
  8086.     else if @procname = 'sp_sproc_columns'
  8087.     begin
  8088.         exec @ret = sp_ddopen;8 @handle output, 
  8089.                             @scrollopt output, 
  8090.                             @ccopt output, 
  8091.                             @rows output, 
  8092.                             @p1,
  8093.                             @p2,
  8094.                             @p3,
  8095.                             @p4,
  8096.                             @ODBCVer
  8097.     end
  8098.     else if @procname = 'sp_statistics'
  8099.     begin
  8100.         exec @ret = sp_ddopen;9 @handle output, 
  8101.                             @scrollopt output, 
  8102.                             @ccopt output, 
  8103.                             @rows output, 
  8104.                             @p1,
  8105.                             @p2,
  8106.                             @p3,
  8107.                             @p4,
  8108.                             @p5,
  8109.                             @p6
  8110.     end
  8111.     else if @procname = 'sp_stored_procedures'
  8112.     begin
  8113.         exec @ret = sp_ddopen;10 @handle output, 
  8114.                              @scrollopt output, 
  8115.                              @ccopt output, 
  8116.                              @rows output, 
  8117.                              @p1,
  8118.                              @p2,
  8119.                              @p3
  8120.     end
  8121.     else if @procname = 'sp_table_privileges'
  8122.     begin
  8123.         exec @ret = sp_ddopen;11 @handle output, 
  8124.                              @scrollopt output, 
  8125.                              @ccopt output, 
  8126.                              @rows output, 
  8127.                              @p1,
  8128.                              @p2,
  8129.                              @p3
  8130.     end
  8131.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  8132.     begin
  8133.         exec @ret = sp_ddopen;12 @handle output,
  8134.                             @procname,
  8135.                             @scrollopt output, 
  8136.                             @ccopt output, 
  8137.                             @rows output, 
  8138.                             @p1,
  8139.                             @p2,
  8140.                             @p3,
  8141.                             @p4,
  8142.                             @p5
  8143.     end
  8144.     else
  8145.         print 'Unknown dd_open procedure'
  8146.     select @ret = isnull(@ret,0)
  8147.     return isnull(@ret,0)
  8148. go
  8149.  
  8150.  
  8151. if (charindex('7.00', @@version) = 0)
  8152. begin
  8153.     print ''
  8154.     print ''
  8155.     print 'Warning:'
  8156.     print 'you are installing the stored procedures '
  8157.     print 'on a pre 7.0 SQL Server.'
  8158.     print 'Ignore the following errors.'
  8159. end
  8160. go
  8161.  
  8162. create procedure sp_ddopen; 2(
  8163.                @handle            int output,
  8164.                @scrollopt        int output,
  8165.                @ccopt            int output,
  8166.                @rows            int output,
  8167.                @p1                nvarchar(774),
  8168.                @p2                nvarchar(774),
  8169.                @p3                nvarchar(774),
  8170.                @p4                nvarchar(774))
  8171. as
  8172.     set nocount on
  8173.     declare @ret int
  8174.  
  8175.     create table #spcolpriv (
  8176.         TABLE_QUALIFIER sysname null,
  8177.         TABLE_OWNER sysname null,
  8178.         TABLE_NAME sysname not null,
  8179.         COLUMN_NAME sysname not null,
  8180.         GRANTOR sysname null,
  8181.         GRANTEE sysname not null,
  8182.         PRIVILEGE varchar(32) not null,
  8183.         IS_GRANTABLE varchar(3) null
  8184.         )
  8185.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  8186.     exec @ret = sp_cursoropen @handle output,
  8187.         'select * from #spcolpriv',
  8188.         @scrollopt output, @ccopt output, @rows output
  8189.     drop table #spcolpriv
  8190.     return @ret
  8191. go
  8192.  
  8193.  
  8194. if (charindex('7.00', @@version) = 0)
  8195. begin
  8196.     print ''
  8197.     print ''
  8198.     print 'Warning:'
  8199.     print 'you are installing the stored procedures '
  8200.     print 'on a pre 7.0 SQL Server.'
  8201.     print 'Ignore the following errors.'
  8202. end
  8203. go
  8204.  
  8205. create procedure sp_ddopen; 3(
  8206.                @handle            int output,
  8207.                @procname        sysname,
  8208.                @scrollopt        int output,
  8209.                @ccopt            int output,
  8210.                @rows            int output,
  8211.                @p1                nvarchar(774),
  8212.                @p2                nvarchar(774),
  8213.                @p3                nvarchar(774),
  8214.                @p4                nvarchar(774),
  8215.                @p5                nvarchar(774),
  8216.                @ODBCVer         int)
  8217. as
  8218.     set nocount on
  8219.     declare @ret int
  8220.  
  8221.     create table #spcolumns (
  8222.         TABLE_QUALIFIER sysname null,
  8223.         TABLE_OWNER sysname null,
  8224.         TABLE_NAME sysname not null,
  8225.         COLUMN_NAME sysname not null,
  8226.         DATA_TYPE smallint not null,
  8227.         TYPE_NAME sysname not null,
  8228.         "PRECISION" int null,
  8229.         LENGTH int null,
  8230.         SCALE smallint null,
  8231.         RADIX smallint null,
  8232.         NULLABLE smallint not null,
  8233.         REMARKS varchar(254) null,
  8234.         COLUMN_DEF nvarchar(3000) null,
  8235.         SQL_DATA_TYPE smallint not null,
  8236.         SQL_DATETIME_SUB smallint null,
  8237.         CHAR_OCTET_LENGTH int null,
  8238.         ORDINAL_POSITION int not null,
  8239.         IS_NULLABLE varchar(254) null,
  8240.         SS_DATA_TYPE tinyint null
  8241.         )
  8242.  
  8243.     if @procname = 'sp_columns'
  8244.     begin
  8245.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  8246.     end
  8247.     else
  8248.     begin
  8249.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  8250.     end
  8251.     exec @ret = sp_cursoropen @handle output,
  8252.         'select * from #spcolumns',
  8253.         @scrollopt output, @ccopt output, @rows output
  8254.     drop table #spcolumns
  8255.     return @ret
  8256. go
  8257.  
  8258.  
  8259.  
  8260. if (charindex('7.00', @@version) = 0)
  8261. begin
  8262.     print ''
  8263.     print ''
  8264.     print 'Warning:'
  8265.     print 'you are installing the stored procedures '
  8266.     print 'on a pre 7.0 SQL Server.'
  8267.     print 'Ignore the following errors.'
  8268. end
  8269. go
  8270.  
  8271. create procedure sp_ddopen; 4(
  8272.                @handle            int output,
  8273.                @scrollopt        int output,
  8274.                @ccopt            int output,
  8275.                @rows            int output,
  8276.                @p7                int,
  8277.                @ODBCVer         int)
  8278. as
  8279.     set nocount on
  8280.     declare @ret int
  8281.  
  8282.     create table #spdatatypeinfo (
  8283.         TYPE_NAME            sysname  not null,
  8284.         DATA_TYPE            smallint not null,
  8285.         "PRECISION"            int null,
  8286.         LITERAL_PREFIX        varchar(32)    null,
  8287.         LITERAL_SUFFIX        varchar(32)    null,
  8288.         CREATE_PARAMS        varchar(32)    null,
  8289.         NULLABLE            smallint   not null,
  8290.         CASE_SENSITIVE        smallint   not null,
  8291.         SEARCHABLE            smallint   not null,
  8292.         UNSIGNED_ATTRIBUTE    smallint   null,
  8293.         MONEY    smallint    not null,
  8294.         AUTO_INCREMENT        smallint    null,
  8295.         LOCAL_TYPE_NAME     sysname null,
  8296.         MINIMUM_SCALE        smallint     null,
  8297.         MAXIMUM_SCALE        smallint   null,
  8298.         SQL_DATA_TYPE        smallint      not null,
  8299.         SQL_DATETIME_SUB    smallint   null,
  8300.         NUM_PREC_RADIX        int     null,
  8301.         INTERVAL_PRECISION    smallint    NULL,
  8302.         USERTYPE            smallint not null)
  8303.  
  8304.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  8305.     exec @ret = sp_cursoropen @handle output,
  8306.         'select * from #spdatatypeinfo',
  8307.         @scrollopt output, @ccopt output, @rows output
  8308.     drop table #spdatatypeinfo
  8309.     return @ret
  8310. go
  8311.  
  8312.  
  8313.  
  8314. if (charindex('7.00', @@version) = 0)
  8315. begin
  8316.     print ''
  8317.     print ''
  8318.     print 'Warning:'
  8319.     print 'you are installing the stored procedures '
  8320.     print 'on a pre 7.0 SQL Server.'
  8321.     print 'Ignore the following errors.'
  8322. end
  8323. go
  8324.  
  8325. create procedure sp_ddopen; 5(
  8326.                @handle            int output,
  8327.                @scrollopt        int output,
  8328.                @ccopt            int output,
  8329.                @rows            int output,
  8330.                @p1                nvarchar(774),
  8331.                @p2                nvarchar(774),
  8332.                @p3                nvarchar(774),
  8333.                @p4                nvarchar(774),
  8334.                @p5                nvarchar(774),
  8335.                @p6                nvarchar(774))
  8336. as
  8337.     set nocount on
  8338.     declare @ret int
  8339.  
  8340.     create table #spfkeys (
  8341.         PKTABLE_QUALIFIER sysname     null,
  8342.         PKTABLE_OWNER sysname    null,
  8343.         PKTABLE_NAME sysname  not null,
  8344.         PKCOLUMN_NAME sysname  not null,
  8345.         FKTABLE_QUALIFIER sysname    null,
  8346.         FKTABLE_OWNER sysname    null,
  8347.         FKTABLE_NAME sysname  not null,
  8348.         FKCOLUMN_NAME sysname  not null,
  8349.         KEY_SEQ smallint not null,
  8350.         UPDATE_RULE smallint null,
  8351.         DELETE_RULE smallint null,
  8352.         FK_NAME sysname null,
  8353.         PK_NAME sysname null,
  8354.         DEFERRABILITY smallint null
  8355.         )
  8356.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  8357.     exec @ret = sp_cursoropen @handle output,
  8358.         'select * from #spfkeys',
  8359.         @scrollopt output, @ccopt output, @rows output
  8360.     drop table #spfkeys
  8361.     return @ret
  8362. go
  8363.  
  8364.  
  8365.  
  8366. if (charindex('7.00', @@version) = 0)
  8367. begin
  8368.     print ''
  8369.     print ''
  8370.     print 'Warning:'
  8371.     print 'you are installing the stored procedures '
  8372.     print 'on a pre 7.0 SQL Server.'
  8373.     print 'Ignore the following errors.'
  8374. end
  8375. go
  8376.  
  8377. create procedure sp_ddopen; 6(
  8378.                @handle            int output,
  8379.                @scrollopt        int output,
  8380.                @ccopt            int output,
  8381.                @rows            int output,
  8382.                @p1                nvarchar(774),
  8383.                @p2                nvarchar(774),
  8384.                @p3                nvarchar(774))
  8385. as
  8386.     set nocount on
  8387.     declare @ret int
  8388.  
  8389.     create table #sppkeys (
  8390.         TABLE_QUALIFIER sysname   null,
  8391.         TABLE_OWNER sysname   null,
  8392.         TABLE_NAME sysname    not null,
  8393.         COLUMN_NAME sysname  not null,
  8394.         KEY_SEQ smallint not null,
  8395.         PK_NAME sysname null
  8396.         )
  8397.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  8398.     exec @ret = sp_cursoropen @handle output,
  8399.         'select * from #sppkeys',
  8400.         @scrollopt output, @ccopt output, @rows output
  8401.     drop table #sppkeys
  8402.     return @ret
  8403. go
  8404.  
  8405.  
  8406.  
  8407. if (charindex('7.00', @@version) = 0)
  8408. begin
  8409.     print ''
  8410.     print ''
  8411.     print 'Warning:'
  8412.     print 'you are installing the stored procedures '
  8413.     print 'on a pre 7.0 SQL Server.'
  8414.     print 'Ignore the following errors.'
  8415. end
  8416. go
  8417.  
  8418. create procedure sp_ddopen; 7(
  8419.                @handle            int output,
  8420.                @scrollopt        int output,
  8421.                @ccopt            int output,
  8422.                @rows            int output,
  8423.                @p1                nvarchar(774),
  8424.                @p2                nvarchar(774),
  8425.                @p3                nvarchar(774),
  8426.                @p4                nvarchar(774),
  8427.                @p5                nvarchar(774),
  8428.                @p6                nvarchar(774),
  8429.                @ODBCVer         int)
  8430. as
  8431.     set nocount on
  8432.     declare @ret int
  8433.  
  8434.     create table #spspeccol (
  8435.         SCOPE smallint null,
  8436.         COLUMN_NAME sysname not null,
  8437.         DATA_TYPE smallint not null,
  8438.         TYPE_NAME sysname not null,
  8439.         "PRECISION" int null,
  8440.         LENGTH int null,
  8441.         SCALE smallint null,
  8442.         PSEUDO_COLUMN smallint null
  8443.         )
  8444.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  8445.     exec @ret = sp_cursoropen @handle output,
  8446.         'select * from #spspeccol',
  8447.         @scrollopt output, @ccopt output, @rows output
  8448.     drop table #spspeccol
  8449.     return @ret
  8450. go
  8451.  
  8452.  
  8453.  
  8454. if (charindex('7.00', @@version) = 0)
  8455. begin
  8456.     print ''
  8457.     print ''
  8458.     print 'Warning:'
  8459.     print 'you are installing the stored procedures '
  8460.     print 'on a pre 7.0 SQL Server.'
  8461.     print 'Ignore the following errors.'
  8462. end
  8463. go
  8464.  
  8465. create procedure sp_ddopen; 8(
  8466.                @handle            int output,
  8467.                @scrollopt        int output,
  8468.                @ccopt            int output,
  8469.                @rows            int output,
  8470.                @p1                nvarchar(774),
  8471.                @p2                nvarchar(774),
  8472.                @p3                nvarchar(774),
  8473.                @p4                nvarchar(774),
  8474.                @ODBCVer         int)
  8475. as
  8476.     set nocount on
  8477.     declare @ret int
  8478.  
  8479.     create table #spproccol (
  8480.         PROCEDURE_QUALIFIER sysname  null,
  8481.         PROCEDURE_OWNER sysname  null,
  8482.         PROCEDURE_NAME sysname not null,
  8483.         COLUMN_NAME sysname not null,
  8484.         COLUMN_TYPE smallint not null,
  8485.         DATA_TYPE smallint not null,
  8486.         TYPE_NAME sysname not null,
  8487.         "PRECISION" int null,
  8488.         LENGTH int null,
  8489.         SCALE smallint null,
  8490.         RADIX smallint null,
  8491.         NULLABLE smallint not null,
  8492.         REMARKS varchar(254) null,
  8493.         COLUMN_DEF nvarchar(3000) null,
  8494.         SQL_DATA_TYPE smallint not null,
  8495.         SQL_DATETIME_SUB smallint null,
  8496.         CHAR_OCTET_LENGTH int null,
  8497.         ORDINAL_POSITION int not null,
  8498.         IS_NULLABLE varchar(254) null,
  8499.         SS_DATA_TYPE tinyint null
  8500.         )
  8501.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  8502.     exec @ret = sp_cursoropen @handle output,
  8503.         'select * from #spproccol',
  8504.         @scrollopt output, @ccopt output, @rows output
  8505.     drop table #spproccol
  8506.     return @ret
  8507. go
  8508.  
  8509.  
  8510.  
  8511. if (charindex('7.00', @@version) = 0)
  8512. begin
  8513.     print ''
  8514.     print ''
  8515.     print 'Warning:'
  8516.     print 'you are installing the stored procedures '
  8517.     print 'on a pre 7.0 SQL Server.'
  8518.     print 'Ignore the following errors.'
  8519. end
  8520. go
  8521.  
  8522. create procedure sp_ddopen; 9(
  8523.                @handle            int output,
  8524.                @scrollopt        int output,
  8525.                @ccopt            int output,
  8526.                @rows            int output,
  8527.                @p1                nvarchar(774),
  8528.                @p2                nvarchar(774),
  8529.                @p3                nvarchar(774),
  8530.                @p4                nvarchar(774),
  8531.                @p5                nvarchar(774),
  8532.                @p6                nvarchar(774))
  8533. as
  8534.     set nocount on
  8535.     declare @ret int
  8536.  
  8537.     create table #spstatistics (
  8538.         TABLE_QUALIFIER sysname   null,
  8539.         TABLE_OWNER sysname   null,
  8540.         TABLE_NAME sysname    not null,
  8541.         NON_UNIQUE smallint null,
  8542.         INDEX_QUALIFIER sysname null,
  8543.         INDEX_NAME sysname null,
  8544.         TYPE smallint not null,
  8545.         SEQ_IN_INDEX smallint null,
  8546.         COLUMN_NAME sysname null,
  8547.         COLLATION char(1) null,
  8548.         CARDINALITY int null,
  8549.         PAGES int null,
  8550.         FILTER_CONDITION varchar(128) null
  8551.         )
  8552.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  8553.     exec @ret = sp_cursoropen @handle output,
  8554.         'select * from #spstatistics',
  8555.         @scrollopt output, @ccopt output, @rows output
  8556.     drop table #spstatistics
  8557.     return @ret
  8558. go
  8559.                
  8560.  
  8561.  
  8562. if (charindex('7.00', @@version) = 0)
  8563. begin
  8564.     print ''
  8565.     print ''
  8566.     print 'Warning:'
  8567.     print 'you are installing the stored procedures '
  8568.     print 'on a pre 7.0 SQL Server.'
  8569.     print 'Ignore the following errors.'
  8570. end
  8571. go
  8572.  
  8573. create procedure sp_ddopen; 10(
  8574.                @handle            int output,
  8575.                @scrollopt        int output,
  8576.                @ccopt            int output,
  8577.                @rows            int output,
  8578.                @p1                nvarchar(774),
  8579.                @p2                nvarchar(774),
  8580.                @p3                nvarchar(774))
  8581. as
  8582.     set nocount on
  8583.     declare @ret int
  8584.  
  8585.     create table #spprocedures (
  8586.         PROCEDURE_QUALIFIER sysname  null,
  8587.         PROCEDURE_OWNER sysname  null,
  8588.         PROCEDURE_NAME sysname not null,
  8589.         NUM_INPUT_PARAMS int null,
  8590.         NUM_OUTPUT_PARAMS int null,
  8591.         NUM_RESULT_SETS int null,
  8592.         REMARKS varchar(254) null,
  8593.         PROCEDURE_TYPE smallint null
  8594.         )
  8595.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  8596.     exec @ret = sp_cursoropen @handle output,
  8597.         'select * from #spprocedures',
  8598.         @scrollopt output, @ccopt output, @rows output
  8599.     drop table #spprocedures
  8600.     return @ret
  8601. go
  8602.  
  8603.  
  8604.  
  8605. if (charindex('7.00', @@version) = 0)
  8606. begin
  8607.     print ''
  8608.     print ''
  8609.     print 'Warning:'
  8610.     print 'you are installing the stored procedures '
  8611.     print 'on a pre 7.0 SQL Server.'
  8612.     print 'Ignore the following errors.'
  8613. end
  8614. go
  8615.  
  8616. create procedure sp_ddopen; 11(
  8617.                @handle            int output,
  8618.                @scrollopt        int output,
  8619.                @ccopt            int output,
  8620.                @rows            int output,
  8621.                @p1                nvarchar(774),
  8622.                @p2                nvarchar(774),
  8623.                @p3                nvarchar(774))
  8624. as
  8625.     set nocount on
  8626.     declare @ret int
  8627.  
  8628.     create table #sptabpriv (
  8629.         TABLE_QUALIFIER sysname null,
  8630.         TABLE_OWNER sysname null,
  8631.         TABLE_NAME sysname not null,
  8632.         GRANTOR sysname null,
  8633.         GRANTEE sysname not null,
  8634.         PRIVILEGE varchar(32) not null,
  8635.         IS_GRANTABLE varchar(3) null
  8636.         )
  8637.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  8638.     exec @ret = sp_cursoropen @handle output,
  8639.         'select * from #sptabpriv',
  8640.         @scrollopt output, @ccopt output, @rows output
  8641.     drop table #sptabpriv
  8642.     return @ret
  8643. go
  8644.  
  8645.  
  8646.  
  8647. if (charindex('7.00', @@version) = 0)
  8648. begin
  8649.     print ''
  8650.     print ''
  8651.     print 'Warning:'
  8652.     print 'you are installing the stored procedures '
  8653.     print 'on a pre 7.0 SQL Server.'
  8654.     print 'Ignore the following errors.'
  8655. end
  8656. go
  8657.  
  8658. create procedure sp_ddopen; 12(
  8659.                @handle            int output,
  8660.                @procname        sysname,
  8661.                @scrollopt        int output,
  8662.                @ccopt            int output,
  8663.                @rows            int output,
  8664.                @p1                nvarchar(774),
  8665.                @p2                nvarchar(774),
  8666.                @p3                nvarchar(774),
  8667.                @p4                nvarchar(774),
  8668.                @p5                nvarchar(774))
  8669. as
  8670.     set nocount on
  8671.     declare @ret int
  8672.  
  8673.     create table #sptables (
  8674.         TABLE_QUALIFIER sysname null,
  8675.         TABLE_OWNER sysname null,
  8676.         TABLE_NAME sysname null,
  8677.         TABLE_TYPE    varchar(32) null,
  8678.         REMARKS varchar(254) null)
  8679.     if @procname = 'sp_tables'
  8680.     begin
  8681.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  8682.     end
  8683.     else
  8684.     begin
  8685.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  8686.     end
  8687.     exec @ret = sp_cursoropen @handle output,
  8688.         'select * from #sptables',
  8689.         @scrollopt output, @ccopt output, @rows output
  8690.     drop table #sptables
  8691.     return @ret
  8692. go
  8693.  
  8694. grant execute on sp_ddopen to public
  8695. go
  8696.  
  8697. print 'creating sp_tableswc'
  8698. go
  8699.  
  8700. if (charindex('7.00', @@version) = 0)
  8701. begin
  8702.     print ''
  8703.     print ''
  8704.     print 'Warning:'
  8705.     print 'you are installing the stored procedures '
  8706.     print 'on a pre 7.0 SQL Server.'
  8707.     print 'Ignore the following errors.'
  8708. end
  8709. go
  8710.  
  8711. /*    Procedure for 7.00 server */
  8712. create procedure sp_tableswc(
  8713.                @table_name        nvarchar(384)    = null,
  8714.                @table_owner     nvarchar(384)    = null,
  8715.                @table_qualifier sysname    = null,
  8716.                @table_type        varchar(100) = null)
  8717. as
  8718.     declare @databasename    sysname
  8719.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  8720.  
  8721.     create table #sptables (
  8722.         TABLE_QUALIFIER sysname null,
  8723.         TABLE_OWNER sysname null,
  8724.         TABLE_NAME sysname null,
  8725.         TABLE_TYPE    varchar(32) null,
  8726.         REMARKS varchar(254) null)
  8727.  
  8728.     declare databases CURSOR FOR
  8729.         select name from master..sysdatabases
  8730.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  8731.         for read only
  8732.  
  8733.     open databases
  8734.     fetch next from databases into @databasename
  8735.     while (@@FETCH_STATUS <> -1)
  8736.     begin
  8737.         if (charindex('%', @databasename) = 0 and
  8738.             charindex('_', @databasename) = 0)
  8739.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  8740.             select @qualprocname = @databasename + '..sp_tables'
  8741.             insert into #sptables exec @qualprocname
  8742.                 @table_name, @table_owner, @databasename, @table_type
  8743.         end
  8744.         fetch next from databases into @databasename
  8745.     end
  8746.     deallocate databases
  8747.     select * from #sptables
  8748.         order by 4, 1, 2, 3
  8749. go
  8750.  
  8751. grant execute on sp_tableswc to public
  8752. go
  8753.  
  8754. dump tran master with no_log
  8755. go
  8756.  
  8757. /*-------------------------------------------------------------------------*/
  8758. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  8759. /*-------------------------------------------------------------------------*/
  8760.  
  8761. print ''
  8762. print 'creating spt_provider_types'
  8763. go
  8764. if (charindex('7.00', @@version) = 0)
  8765. begin /* Pre 7.00 Server */
  8766. create table spt_provider_types
  8767.     (
  8768.     ss_dtype        tinyint     not null,
  8769.     fixlen            int         null,        /* datatype len for variable, else null */
  8770.     type_name        sysname        not null,
  8771.     oledb_data_type        smallint    not null,
  8772.     best_match        bit        not null,
  8773.     is_nullable        tinyint        null,
  8774.     case_sensitive        bit        not null,
  8775.     fixed_prec_scale    bit        not null,
  8776.     is_long            bit        not null,
  8777.     auto_unique_value    tinyint        not null,
  8778.     data_precision        int         null,
  8779.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8780.     column_size        int         null,
  8781.     literal_prefix        varchar(32)    null,
  8782.     literal_suffix        varchar(32)     null,
  8783.     searchable        int        not null,
  8784.     unsigned_attribute    tinyint        null,
  8785.     local_type_name     sysname        null
  8786.     )
  8787. print ''
  8788. print ''
  8789. print 'Warning:'
  8790. print 'you are installing the stored procedures '
  8791. print 'on a pre 7.0 SQL Server.'
  8792. print 'Ignore the following errors.'
  8793. end
  8794. go
  8795. if (charindex('7.00', @@version) > 0)
  8796. begin /* 7.00 server*/
  8797. create table spt_provider_types
  8798.     (
  8799.     ss_dtype        tinyint     not null,
  8800.     fixlen            int         null,        /* datatype len for variable, else null */
  8801.     type_name        sysname        not null,
  8802.     oledb_data_type        smallint    not null,
  8803.     best_match        bit        not null,
  8804.     is_nullable        bit        null,
  8805.     case_sensitive        bit        not null,
  8806.     fixed_prec_scale    bit        not null,
  8807.     is_long            bit        not null,
  8808.     auto_unique_value    tinyint        not null,
  8809.     data_precision        int         null,
  8810.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8811.     column_size        int         null,
  8812.     literal_prefix        nvarchar(32)    null,
  8813.     literal_suffix        nvarchar(32)     null,
  8814.     searchable        int        not null,
  8815.     unsigned_attribute    tinyint        null,
  8816.     local_type_name     sysname        null
  8817.     )
  8818. end
  8819. go
  8820.  
  8821. grant select on spt_provider_types to public
  8822. go
  8823. dump tran master with no_log
  8824. go
  8825.  
  8826. /*
  8827. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  8828. */
  8829. begin tran
  8830.  
  8831. /* Get case sensitivity */
  8832. declare @case_sensitive bit
  8833. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  8834. /* Local Char */
  8835. insert into spt_provider_types values
  8836.     (
  8837.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  8838.     0,                /* fixlen */
  8839.     'char',                /* type_name */
  8840.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8841.     0,                /* best_match */
  8842.     null,                /* is_nullable */
  8843.     @case_sensitive,        /* case_sensitive */
  8844.     0,                /* fixed_prec_scale */
  8845.     0,                /* is_long */
  8846.     0,                /* auto_unique_value */
  8847.     null,                /* data_precision */
  8848.     null,                /* numeric_scale */
  8849.     null,                /* column_size */
  8850.     '''',                /* literal_prefix */
  8851.     '''',                /* literal_suffix */
  8852.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8853.     null,                /* unsigned_attribute */
  8854.     'char'                /* local_type_name */
  8855.     )
  8856.  
  8857. /* Local Varchar */
  8858. insert into spt_provider_types values
  8859.     (
  8860.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  8861.     null,                /* fixlen */
  8862.     'varchar',            /* type_name */
  8863.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8864.     1,                /* best_match */
  8865.     null,                /* is_nullable */
  8866.     @case_sensitive,        /* case_sensitive */
  8867.     0,                /* fixed_prec_scale */
  8868.     0,                /* is_long */
  8869.     0,                /* auto_unique_value */
  8870.     null,                /* data_precision */
  8871.     null,                /* numeric_scale */
  8872.     null,                /* column_size */
  8873.     '''',                /* literal_prefix */
  8874.     '''',                /* literal_suffix */
  8875.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8876.     null,                /* unsigned_attribute */
  8877.     'varchar'            /* local_type_name */
  8878.     )
  8879.  
  8880. /* Local Text */
  8881. insert into spt_provider_types values
  8882.     (
  8883.     35  /*SQLTEXT*/,        /* ss_dtype */
  8884.     null,                /* fixlen */
  8885.     'text',                /* type_name */
  8886.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8887.     0,                /* best_match */
  8888.     null,                /* is_nullable */
  8889.     @case_sensitive,        /* case_sensitive */
  8890.     0,                /* fixed_prec_scale */
  8891.     1,                /* is_long */
  8892.     0,                /* auto_unique_value */
  8893.     null,                /* data_precision */
  8894.     null,                /* numeric_scale */
  8895.     2147483647,            /* column_size */
  8896.     '''',                /* literal_prefix */
  8897.     '''',                /* literal_suffix */
  8898.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  8899.     null,                /* unsigned_attribute */
  8900.     'text'                /* local_type_name */
  8901.     )
  8902. commit tran
  8903. go
  8904. dump tran master with no_log
  8905. go
  8906.  
  8907. /*
  8908. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  8909. */
  8910. begin tran
  8911. /* Local Binary */
  8912. insert into spt_provider_types values
  8913.     (
  8914.     45 /*SQLBINARY*/,        /* ss_dtype */
  8915.     0,                /* fixlen */
  8916.     'binary',            /* type_name */
  8917.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8918.     0,                /* best_match */
  8919.     null,                /* is_nullable */
  8920.     0,                /* case_sensitive */
  8921.     0,                /* fixed_prec_scale */
  8922.     0,                /* is_long */
  8923.     0,                /* auto_unique_value */
  8924.     null,                /* data_precision */
  8925.     null,                /* numeric_scale */
  8926.     null,                /* column_size */
  8927.     '0x',                /* literal_prefix */
  8928.     null,                /* literal_suffix */
  8929.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8930.     null,                /* unsigned_attribute */
  8931.     'binary'            /* local_type_name */
  8932.     )
  8933.  
  8934. /* Local Varbinary */
  8935. insert into spt_provider_types values
  8936.     (
  8937.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  8938.     null,                /* fixlen */
  8939.     'varbinary',            /* type_name */
  8940.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8941.     1,                /* best_match */
  8942.     null,                /* is_nullable */
  8943.     0,                /* case_sensitive */
  8944.     0,                /* fixed_prec_scale */
  8945.     0,                /* is_long */
  8946.     0,                /* auto_unique_value */
  8947.     null,                /* data_precision */
  8948.     null,                /* numeric_scale */
  8949.     null,                /* column_size */
  8950.     '0x',                /* literal_prefix */
  8951.     null,                /* literal_suffix */
  8952.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8953.     null,                /* unsigned_attribute */
  8954.     'varbinary'            /* local_type_name */
  8955.     )
  8956.  
  8957. /* Local Image */
  8958. insert into spt_provider_types values
  8959.     (
  8960.     34 /*SQLIMAGE*/,        /* ss_dtype */
  8961.     null,                /* fixlen */
  8962.     'image',            /* type_name */
  8963.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8964.     0,                /* best_match */
  8965.     null,                /* is_nullable */
  8966.     0,                /* case_sensitive */
  8967.     0,                /* fixed_prec_scale */
  8968.     1,                /* is_long */
  8969.     0,                /* auto_unique_value */
  8970.     null,                /* data_precision */
  8971.     null,                /* numeric_scale */
  8972.     2147483647,            /* column_size */
  8973.     '0x',                /* literal_prefix */
  8974.     null,                /* literal_suffix */
  8975.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  8976.     null,                /* unsigned_attribute */
  8977.     'image'                /* local_type_name */
  8978.     )
  8979.  
  8980. commit tran
  8981. go
  8982. dump tran master with no_log
  8983. go
  8984.  
  8985. /*
  8986. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  8987. */
  8988. begin tran
  8989.  
  8990. /* Local Datetime */
  8991. insert into spt_provider_types values
  8992.     (
  8993.     61 /*SQLDATETIME*/,        /* ss_dtype */
  8994.     8,                /* fixlen */
  8995.     'datetime',            /* type_name */
  8996.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8997.     1,                /* best_match */
  8998.     null,                /* is_nullable */
  8999.     0,                /* case_sensitive */
  9000.     0,                /* fixed_prec_scale */
  9001.     0,                /* is_long */
  9002.     0,                /* auto_unique_value */
  9003.     23,                /* data_precision */
  9004.     null,                /* numeric_scale */
  9005.     null,                /* column_size */
  9006.     '''',                /* literal_prefix */
  9007.     '''',                /* literal_suffix */
  9008.     4 /*DB_SEARCHABLE*/,        /* searchable */
  9009.     null,                /* unsigned_attribute */
  9010.     'datetime'            /* local_type_name */
  9011.     )
  9012.  
  9013. /* Local Smalldatetime */
  9014. insert into spt_provider_types values
  9015.     (
  9016.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  9017.     4,                /* fixlen */
  9018.     'smalldatetime',        /* type_name */
  9019.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9020.     0,                /* best_match */
  9021.     null,                /* is_nullable */
  9022.     0,                /* case_sensitive */
  9023.     0,                /* fixed_prec_scale */
  9024.     0,                /* is_long */
  9025.     0,                /* auto_unique_value */
  9026.     16,                /* data_precision */
  9027.     null,                /* numeric_scale */
  9028.     null,                /* column_size */
  9029.     '''',                /* literal_prefix */
  9030.     '''',                /* literal_suffix */
  9031.     4 /*DB_SEARCHABLE*/,        /* searchable */
  9032.     null,                /* unsigned_attribute */
  9033.     'smalldatetime'            /* local_type_name */
  9034.     )
  9035.  
  9036. if (charindex('7.00', @@version) = 0)
  9037.     begin    /*    Add nullable type for non-Sphinx server */
  9038.     
  9039.     /* Local Datetimn */
  9040.     insert into spt_provider_types values
  9041.         (
  9042.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  9043.         4,                /* fixlen */
  9044.         'smalldatetime',        /* type_name */
  9045.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9046.         0,                /* best_match */
  9047.         null,                /* is_nullable */
  9048.         0,                /* case_sensitive */
  9049.         0,                /* fixed_prec_scale */
  9050.         0,                /* is_long */
  9051.         0,                /* auto_unique_value */
  9052.         16,                /* data_precision */
  9053.         null,                /* numeric_scale */
  9054.         null,                /* column_size */
  9055.         '''',                /* literal_prefix */
  9056.         '''',                /* literal_suffix */
  9057.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9058.         null,                /* unsigned_attribute */
  9059.         'smalldatetime'            /* local_type_name */
  9060.         )    
  9061.     insert into spt_provider_types values
  9062.         (
  9063.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  9064.         8,                /* fixlen */
  9065.         'datetime',            /* type_name */
  9066.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  9067.         0,                /* best_match */
  9068.         null,                /* is_nullable */
  9069.         0,                /* case_sensitive */
  9070.         0,                /* fixed_prec_scale */
  9071.         0,                /* is_long */
  9072.         0,                /* auto_unique_value */
  9073.         23,                /* data_precision */
  9074.         null,                /* numeric_scale */
  9075.         null,                /* column_size */
  9076.         '''',                /* literal_prefix */
  9077.         '''',                /* literal_suffix */
  9078.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9079.         null,                /* unsigned_attribute */
  9080.         'datetime'            /* local_type_name */
  9081.         )
  9082.     end
  9083.  
  9084. commit tran
  9085. go
  9086. dump tran master with no_log
  9087. go
  9088.  
  9089. /*
  9090. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  9091. */
  9092. begin tran
  9093.  
  9094. /* Local Smallmoney */
  9095. insert into spt_provider_types values
  9096.     (
  9097.     122 /*SQLMONEY4*/,        /* ss_dtype */
  9098.     4,                /* fixlen */
  9099.     'smallmoney',            /* type_name */
  9100.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  9101.     0,                /* best_match */
  9102.     null,                /* is_nullable */
  9103.     0,                /* case_sensitive */
  9104.     1,                /* fixed_prec_scale */
  9105.     0,                /* is_long */
  9106.     0,                /* auto_unique_value */
  9107.     10,                /* data_precision */
  9108.     null,                /* numeric_scale */
  9109.     null,                /* column_size */
  9110.     '$',                /* literal_prefix */
  9111.     null,                /* literal_suffix */
  9112.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9113.     0,                /* unsigned_attribute */
  9114.     'smallmoney'            /* local_type_name */
  9115.     )    
  9116.     
  9117. /* Local Money */
  9118. insert into spt_provider_types values
  9119.     (
  9120.     60 /*SQLMONEY*/,        /* ss_dtype */
  9121.     8,                /* fixlen */
  9122.     'money',            /* type_name */
  9123.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  9124.     1,                /* best_match */
  9125.     null,                /* is_nullable */
  9126.     0,                /* case_sensitive */
  9127.     1,                /* fixed_prec_scale */
  9128.     0,                /* is_long */
  9129.     0,                /* auto_unique_value */
  9130.     19,                /* data_precision */
  9131.     null,                /* numeric_scale */
  9132.     null,                /* column_size */
  9133.     '$',                /* literal_prefix */
  9134.     null,                /* literal_suffix */
  9135.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9136.     0,                /* unsigned_attribute */
  9137.     'money'                /* local_type_name */
  9138.     )    
  9139.  
  9140. if (charindex('7.00', @@version) = 0)
  9141.     begin    /*    Add nullable type for non-Sphinx server */
  9142.  
  9143.     /* Local Moneyn */
  9144.     insert into spt_provider_types values
  9145.         (
  9146.         110 /*SQLMONEYN*/,    /* ss_dtype */
  9147.         4,            /* fixlen */
  9148.         'smallmoney',        /* type_name */
  9149.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  9150.         0,            /* best_match */
  9151.         null,            /* is_nullable */
  9152.         0,            /* case_sensitive */
  9153.         1,            /* fixed_prec_scale */
  9154.         0,            /* is_long */
  9155.         0,            /* auto_unique_value */
  9156.         10,            /* data_precision */
  9157.         null,            /* numeric_scale */
  9158.         null,            /* column_size */
  9159.         '$',            /* literal_prefix */
  9160.         null,            /* literal_suffix */
  9161.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  9162.         0,            /* unsigned_attribute */
  9163.         'smallmoney'        /* local_type_name */
  9164.         )    
  9165.     insert into spt_provider_types values
  9166.         (
  9167.         110 /*SQLMONEYN*/,    /* ss_dtype */
  9168.         8,            /* fixlen */
  9169.         'money',        /* type_name */
  9170.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  9171.         0,            /* best_match */
  9172.         null,            /* is_nullable */
  9173.         0,            /* case_sensitive */
  9174.         1,            /* fixed_prec_scale */
  9175.         0,            /* is_long */
  9176.         0,            /* auto_unique_value */
  9177.         19,            /* data_precision */
  9178.         null,            /* numeric_scale */
  9179.         null,            /* column_size */
  9180.         '$',            /* literal_prefix */
  9181.         null,            /* literal_suffix */
  9182.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  9183.         null,            /* unsigned_attribute */
  9184.         'money'            /* local_type_name */
  9185.         )    
  9186.     end
  9187.  
  9188. commit tran
  9189. go
  9190. dump tran master with no_log
  9191. go
  9192.  
  9193. /*
  9194. ** Insert the spt_provider_types rows for the numeric data types
  9195. */
  9196. begin tran
  9197.  
  9198. /* Local Float */
  9199. insert into spt_provider_types values
  9200.     (
  9201.     62 /*SQLFLT8*/,            /* ss_dtype */
  9202.     8,                /* fixlen */
  9203.     'float',            /* type_name */
  9204.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  9205.     1,                /* best_match */
  9206.     null,                /* is_nullable */
  9207.     0,                /* case_sensitive */
  9208.     0,                /* fixed_prec_scale */
  9209.     0,                /* is_long */
  9210.     0,                /* auto_unique_value */
  9211.     15,                /* data_precision */
  9212.     null,                /* numeric_scale */
  9213.     null,                /* column_size */
  9214.     null,                /* literal_prefix */
  9215.     null,                /* literal_suffix */
  9216.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9217.     0,                /* unsigned_attribute */
  9218.     'float'                /* local_type_name */
  9219.     )    
  9220.  
  9221. /* Local Real */
  9222. insert into spt_provider_types values
  9223.     (
  9224.     59 /*SQLFLT4*/,            /* ss_dtype */
  9225.     4,                /* fixlen */
  9226.     'real',                /* type_name */
  9227.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  9228.     1,                /* best_match */
  9229.     null,                /* is_nullable */
  9230.     0,                /* case_sensitive */
  9231.     0,                /* fixed_prec_scale */
  9232.     0,                /* is_long */
  9233.     0,                /* auto_unique_value */
  9234.     7,                /* data_precision */
  9235.     null,                /* numeric_scale */
  9236.     null,                /* column_size */
  9237.     null,                /* literal_prefix */
  9238.     null,                /* literal_suffix */
  9239.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9240.     0,                /* unsigned_attribute */
  9241.     'real'                /* local_type_name */
  9242.     )    
  9243.  
  9244. /* Local Int */
  9245. insert into spt_provider_types values
  9246.     (
  9247.     56 /*SQLINT4*/,            /* ss_dtype */
  9248.     4,                /* fixlen */
  9249.     'int',                /* type_name */
  9250.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  9251.     1,                /* best_match */
  9252.     null,                /* is_nullable */
  9253.     0,                /* case_sensitive */
  9254.     1,                /* fixed_prec_scale */
  9255.     0,                /* is_long */
  9256.     1,                /* auto_unique_value */
  9257.     10,                /* data_precision */
  9258.     null,                /* numeric_scale */
  9259.     null,                /* column_size */
  9260.     null,                /* literal_prefix */
  9261.     null,                /* literal_suffix */
  9262.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9263.     0,                /* unsigned_attribute */
  9264.     'int'                /* local_type_name */
  9265.     )    
  9266.  
  9267. /* Local Smallint */
  9268. insert into spt_provider_types values
  9269.     (
  9270.     52 /*SQLINT2*/,            /* ss_dtype */
  9271.     2,                /* fixlen */
  9272.     'smallint',            /* type_name */
  9273.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  9274.     1,                /* best_match */
  9275.     null,                /* is_nullable */
  9276.     0,                /* case_sensitive */
  9277.     1,                /* fixed_prec_scale */
  9278.     0,                /* is_long */
  9279.     1,                /* auto_unique_value */
  9280.     5,                /* data_precision */
  9281.     null,                /* numeric_scale */
  9282.     null,                /* column_size */
  9283.     null,                /* literal_prefix */
  9284.     null,                /* literal_suffix */
  9285.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9286.     0,                /* unsigned_attribute */
  9287.     'smallint'            /* local_type_name */
  9288.     )    
  9289.  
  9290. /* Local Tinyint */
  9291. insert into spt_provider_types values
  9292.     (
  9293.     48 /*SQLINT1*/,            /* ss_dtype */
  9294.     1,                /* fixlen */
  9295.     'tinyint',            /* type_name */
  9296.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  9297.     1,                /* best_match */
  9298.     null,                /* is_nullable */
  9299.     0,                /* case_sensitive */
  9300.     1,                /* fixed_prec_scale */
  9301.     0,                /* is_long */
  9302.     1,                /* auto_unique_value */
  9303.     3,                /* data_precision */
  9304.     null,                /* numeric_scale */
  9305.     null,                /* column_size */
  9306.     null,                /* literal_prefix */
  9307.     null,                /* literal_suffix */
  9308.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9309.     1,                /* unsigned_attribute */
  9310.     'tinyint'            /* local_type_name */
  9311.     )    
  9312. commit tran
  9313. go
  9314. dump tran master with no_log
  9315. go
  9316. begin tran    
  9317. if (charindex('6.00', @@version) > 0 or
  9318.     charindex('6.50', @@version) > 0 or
  9319.     charindex('7.00', @@version) > 0)
  9320.     begin    /*    Add 6.0 data types */
  9321.     
  9322.     /* Local Decimal */
  9323.     insert into spt_provider_types values
  9324.         (
  9325.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  9326.         0,                /* fixlen */
  9327.         'decimal',            /* type_name */
  9328.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9329.         0,                /* best_match */
  9330.         null,                /* is_nullable */
  9331.         0,                /* case_sensitive */
  9332.         1,                /* fixed_prec_scale */
  9333.         0,                /* is_long */
  9334.         1,                /* auto_unique_value */
  9335.         38,                /* data_precision */
  9336.         null,                /* numeric_scale */
  9337.         null,                /* column_size */
  9338.         null,                /* literal_prefix */
  9339.         null,                /* literal_suffix */
  9340.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9341.         0,                /* unsigned_attribute */
  9342.         'decimal'            /* local_type_name */
  9343.         )    
  9344.  
  9345.     /* Local Numeric */
  9346.     insert into spt_provider_types values
  9347.         (
  9348.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  9349.         0,                /* fixlen */
  9350.         'numeric',            /* type_name */
  9351.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9352.         1,                /* best_match */
  9353.         null,                /* is_nullable */
  9354.         0,                /* case_sensitive */
  9355.         1,                /* fixed_prec_scale */
  9356.         0,                /* is_long */
  9357.         1,                /* auto_unique_value */
  9358.         38,                /* data_precision */
  9359.         null,                /* numeric_scale */
  9360.         null,                /* column_size */
  9361.         null,                /* literal_prefix */
  9362.         null,                /* literal_suffix */
  9363.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9364.         0,                /* unsigned_attribute */
  9365.         'numeric'            /* local_type_name */
  9366.         )    
  9367.     end
  9368. commit tran
  9369. go
  9370. dump tran master with no_log
  9371. go
  9372. begin tran
  9373. if (charindex('7.00', @@version) = 0)
  9374.     begin    /*    Add nullable type for non-Sphinx server */
  9375.     
  9376.     /* Local Floatn */
  9377.     insert into spt_provider_types values
  9378.         (
  9379.         109 /*SQLFLTN*/,        /* ss_dtype */
  9380.         8,                /* fixlen */
  9381.         'float',            /* type_name */
  9382.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  9383.         0,                /* best_match */
  9384.         null,                /* is_nullable */
  9385.         0,                /* case_sensitive */
  9386.         0,                /* fixed_prec_scale */
  9387.         0,                /* is_long */
  9388.         0,                /* auto_unique_value */
  9389.         15,                /* data_precision */
  9390.         null,                /* numeric_scale */
  9391.         null,                /* column_size */
  9392.         null,                /* literal_prefix */
  9393.         null,                /* literal_suffix */
  9394.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9395.         null,                /* unsigned_attribute */
  9396.         'float'                /* local_type_name */
  9397.         )    
  9398.     insert into spt_provider_types values
  9399.         (
  9400.         109 /*SQLFLT4*/,        /* ss_dtype */
  9401.         4,                /* fixlen */
  9402.         'real',                /* type_name */
  9403.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  9404.         0,                /* best_match */
  9405.         null,                /* is_nullable */
  9406.         0,                /* case_sensitive */
  9407.         0,                /* fixed_prec_scale */
  9408.         0,                /* is_long */
  9409.         0,                /* auto_unique_value */
  9410.         7,                /* data_precision */
  9411.         null,                /* numeric_scale */
  9412.         null,                /* column_size */
  9413.         null,                /* literal_prefix */
  9414.         null,                /* literal_suffix */
  9415.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9416.         null,                /* unsigned_attribute */
  9417.         'real'                /* local_type_name */
  9418.         )    
  9419.  
  9420.     /* Local Intn */
  9421.     insert into spt_provider_types values
  9422.         (
  9423.         38 /*SQLINTN*/,            /* ss_dtype */
  9424.         4,                /* fixlen */
  9425.         'int',                /* type_name */
  9426.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  9427.         1,                /* best_match */
  9428.         null,                /* is_nullable */
  9429.         0,                /* case_sensitive */
  9430.         1,                /* fixed_prec_scale */
  9431.         0,                /* is_long */
  9432.         1,                /* auto_unique_value */
  9433.         10,                /* data_precision */
  9434.         null,                /* numeric_scale */
  9435.         null,                /* column_size */
  9436.         null,                /* literal_prefix */
  9437.         null,                /* literal_suffix */
  9438.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9439.         0,                /* unsigned_attribute */
  9440.         'int'                /* local_type_name */
  9441.         )    
  9442.     insert into spt_provider_types values
  9443.         (
  9444.         38 /*SQLINTN*/,            /* ss_dtype */
  9445.         2,                /* fixlen */
  9446.         'smallint',            /* type_name */
  9447.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  9448.         0,                /* best_match */
  9449.         null,                /* is_nullable */
  9450.         0,                /* case_sensitive */
  9451.         1,                /* fixed_prec_scale */
  9452.         0,                /* is_long */
  9453.         1,                /* auto_unique_value */
  9454.         5,                /* data_precision */
  9455.         null,                /* numeric_scale */
  9456.         null,                /* column_size */
  9457.         null,                /* literal_prefix */
  9458.         null,                /* literal_suffix */
  9459.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9460.         0,                /* unsigned_attribute */
  9461.         'smallint'            /* local_type_name */
  9462.         )    
  9463.     insert into spt_provider_types values
  9464.         (
  9465.         38 /*SQLINTN*/,            /* ss_dtype */
  9466.         1,                /* fixlen */
  9467.         'tinyint',            /* type_name */
  9468.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  9469.         0,                /* best_match */
  9470.         null,                /* is_nullable */
  9471.         0,                /* case_sensitive */
  9472.         1,                /* fixed_prec_scale */
  9473.         0,                /* is_long */
  9474.         1,                /* auto_unique_value */
  9475.         3,                /* data_precision */
  9476.         null,                /* numeric_scale */
  9477.         null,                /* column_size */
  9478.         null,                /* literal_prefix */
  9479.         null,                /* literal_suffix */
  9480.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9481.         1,                /* unsigned_attribute */
  9482.         'tinyint'            /* local_type_name */
  9483.         )    
  9484.  
  9485.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  9486.         begin    /*    Add 6.0 data types */
  9487.         
  9488.         /* Local Decimaln */
  9489.         insert into spt_provider_types values
  9490.             (
  9491.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  9492.             0,                /* fixlen */
  9493.             'decimal',            /* type_name */
  9494.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9495.             0,                /* best_match */
  9496.             null,                /* is_nullable */
  9497.             0,                /* case_sensitive */
  9498.             1,                /* fixed_prec_scale */
  9499.             0,                /* is_long */
  9500.             1,                /* auto_unique_value */
  9501.             38,                /* data_precision */
  9502.             null,                /* numeric_scale */
  9503.             null,                /* column_size */
  9504.             null,                /* literal_prefix */
  9505.             null,                /* literal_suffix */
  9506.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9507.             0,                /* unsigned_attribute */
  9508.             'decimal'            /* local_type_name */
  9509.             )    
  9510.  
  9511.         /* Local Numericn */
  9512.         insert into spt_provider_types values
  9513.             (
  9514.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  9515.             0,                /* fixlen */
  9516.             'numeric',            /* type_name */
  9517.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9518.             0,                /* best_match */
  9519.             null,                /* is_nullable */
  9520.             0,                /* case_sensitive */
  9521.             1,                /* fixed_prec_scale */
  9522.             0,                /* is_long */
  9523.             1,                /* auto_unique_value */
  9524.             38,                /* data_precision */
  9525.             null,                /* numeric_scale */
  9526.             null,                /* column_size */
  9527.             null,                /* literal_prefix */
  9528.             null,                /* literal_suffix */
  9529.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9530.             0,                /* unsigned_attribute */
  9531.             'numeric'            /* local_type_name */
  9532.             )    
  9533.         end
  9534.     end
  9535. commit tran
  9536. go
  9537. dump tran master with no_log
  9538. go
  9539.  
  9540. /*
  9541. **    Remaining data types
  9542. */
  9543. if (charindex('7.00', @@version) = 0)
  9544. begin
  9545.     print ''
  9546.     print ''
  9547.     print 'Warning:'
  9548.     print 'you are installing the stored procedures '
  9549.     print 'on a pre 7.0 SQL Server.'
  9550.     print 'Ignore the following errors.'
  9551. end
  9552. go
  9553. begin tran
  9554.  
  9555. if (charindex('7.00', @@version) > 0)
  9556.     begin
  9557.     /* Local Bit */
  9558.     insert into spt_provider_types values
  9559.         (
  9560.         50 /*SQLBIT*/,            /* ss_dtype */
  9561.         0,                /* fixlen */
  9562.         'bit',                /* type_name */
  9563.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9564.         1,                /* best_match */
  9565.         null,                /* is_nullable */
  9566.         0,                /* case_sensitive */
  9567.         0,                /* fixed_prec_scale */
  9568.         0,                /* is_long */
  9569.         0,                /* auto_unique_value */
  9570.         1,                /* data_precision */
  9571.         null,                /* numeric_scale */
  9572.         null,                /* column_size */
  9573.         null,                /* literal_prefix */
  9574.         null,                /* literal_suffix */
  9575.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9576.         null,                /* unsigned_attribute */
  9577.         'bit'                /* local_type_name */
  9578.         )        
  9579.     
  9580.     /* Local Timestamp */
  9581.     insert into spt_provider_types values
  9582.         (
  9583.         0,                /* ss_dtype */
  9584.         8,                /* fixlen */
  9585.         'timestamp',            /* type_name */
  9586.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9587.         0,                /* best_match */
  9588.         null,                /* is_nullable */
  9589.         0,                /* case_sensitive */
  9590.         0,                /* fixed_prec_scale */
  9591.         0,                /* is_long */
  9592.         0,                /* auto_unique_value */
  9593.         null,                /* data_precision */
  9594.         null,                /* numeric_scale */
  9595.         null,                /* column_size */
  9596.         '0x',                /* literal_prefix */
  9597.         null,                /* literal_suffix */
  9598.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9599.         null,                /* unsigned_attribute */
  9600.         'timestamp'            /* local_type_name */
  9601.         )    
  9602.     /* Local GUID */
  9603.     insert into spt_provider_types values
  9604.         (
  9605.         0,                /* ss_dtype */
  9606.         16,                /* fixlen */
  9607.         'uniqueidentifier',        /* type_name */
  9608.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  9609.         1,                /* best_match */
  9610.         null,                /* is_nullable */
  9611.         0,                /* case_sensitive */
  9612.         0,                /* fixed_prec_scale */
  9613.         0,                /* is_long */
  9614.         0,                /* auto_unique_value */
  9615.         null,                /* data_precision */
  9616.         null,                /* numeric_scale */
  9617.         null,                /* column_size */
  9618.         '''',                /* literal_prefix */
  9619.         '''',                /* literal_suffix */
  9620.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9621.         null,                /* unsigned_attribute */
  9622.         'uniqueidentifier'        /* local_type_name */
  9623.         )    
  9624.     
  9625.     /* Get case sensitivity */
  9626.     declare @ncase_sensitive bit
  9627.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9628.  
  9629.     /* Local NChar */
  9630.     insert into spt_provider_types values
  9631.         (
  9632.         0,                /* ss_dtype */
  9633.         0,                /* fixlen */
  9634.         'nchar',            /* type_name */
  9635.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9636.         0,                /* best_match */
  9637.         null,                /* is_nullable */
  9638.         @ncase_sensitive,        /* case_sensitive */
  9639.         0,                /* fixed_prec_scale */
  9640.         0,                /* is_long */
  9641.         0,                /* auto_unique_value */
  9642.         null,                /* data_precision */
  9643.         null,                /* numeric_scale */
  9644.         null,                /* column_size */
  9645.         'N''',                /* literal_prefix */
  9646.         '''',                /* literal_suffix */
  9647.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9648.         null,                /* unsigned_attribute */
  9649.         'nchar'                /* local_type_name */
  9650.         )    
  9651.  
  9652.     /* Local NVarChar */
  9653.     insert into spt_provider_types values
  9654.         (
  9655.         0,                /* ss_dtype */
  9656.         null,                /* fixlen */
  9657.         'nvarchar',            /* type_name */
  9658.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9659.         1,                /* best_match */
  9660.         null,                /* is_nullable */
  9661.         @ncase_sensitive,        /* case_sensitive */
  9662.         0,                /* fixed_prec_scale */
  9663.         0,                /* is_long */
  9664.         0,                /* auto_unique_value */
  9665.         null,                /* data_precision */
  9666.         null,                /* numeric_scale */
  9667.         null,                /* column_size */
  9668.         'N''',                /* literal_prefix */
  9669.         '''',                /* literal_suffix */
  9670.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9671.         null,                /* unsigned_attribute */
  9672.         'nvarchar'            /* local_type_name */
  9673.         )    
  9674.  
  9675.     /* Local NText */
  9676.     insert into spt_provider_types values
  9677.         (
  9678.         0,                /* ss_dtype */
  9679.         null,                /* fixlen */
  9680.         'ntext',            /* type_name */
  9681.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9682.         0,                /* best_match */
  9683.         null,                /* is_nullable */
  9684.         @ncase_sensitive,        /* case_sensitive */
  9685.         0,                /* fixed_prec_scale */
  9686.         1,                /* is_long */
  9687.         0,                /* auto_unique_value */
  9688.         null,                /* data_precision */
  9689.         null,                /* numeric_scale */
  9690.         1073741823,            /* column_size */
  9691.         'N''',                /* literal_prefix */
  9692.         '''',                /* literal_suffix */
  9693.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  9694.         null,                /* unsigned_attribute */
  9695.         'ntext'                /* local_type_name */
  9696.         )    
  9697.  
  9698.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  9699.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  9700.  
  9701.     end
  9702. commit tran
  9703. go
  9704. dump tran master with no_log
  9705. go
  9706. begin tran
  9707. if (charindex('7.00', @@version) = 0)
  9708.     begin
  9709.     /* Local Bit */
  9710.     insert into spt_provider_types values
  9711.         (
  9712.         50 /*SQLBIT*/,            /* ss_dtype */
  9713.         0,                /* fixlen */
  9714.         'bit',                /* type_name */
  9715.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9716.         1,                /* best_match */
  9717.         0,                /* is_nullable */
  9718.         0,                /* case_sensitive */
  9719.         0,                /* fixed_prec_scale */
  9720.         0,                /* is_long */
  9721.         0,                /* auto_unique_value */
  9722.         1,                /* data_precision */
  9723.         null,                /* numeric_scale */
  9724.         null,                /* column_size */
  9725.         null,                /* literal_prefix */
  9726.         null,                /* literal_suffix */
  9727.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9728.         null,                /* unsigned_attribute */
  9729.         'bit'                /* local_type_name */
  9730.         )    
  9731.  
  9732.     /* Local Timestamp */
  9733.     insert into spt_provider_types values
  9734.         (
  9735.         45 /*SQLBINARY*/,        /* ss_dtype */
  9736.         8,                /* fixlen */
  9737.         'timestamp',            /* type_name */
  9738.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9739.         0,                /* best_match */
  9740.         null,                /* is_nullable */
  9741.         0,                /* case_sensitive */
  9742.         0,                /* fixed_prec_scale */
  9743.         0,                /* is_long */
  9744.         0,                /* auto_unique_value */
  9745.         null,                /* data_precision */
  9746.         null,                /* numeric_scale */
  9747.         null,                /* column_size */
  9748.         '0x',                /* literal_prefix */
  9749.         null,                /* literal_suffix */
  9750.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9751.         null,                /* unsigned_attribute */
  9752.         'timestamp'            /* local_type_name */
  9753.         )        
  9754. end
  9755.  
  9756. commit tran
  9757. go
  9758. dump tran master with no_log
  9759. go
  9760.  
  9761. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  9762. go
  9763.  
  9764. dump tran master with no_log
  9765. go
  9766. if (charindex('6.00', @@version) > 0)
  9767.     begin
  9768.     if (exists (select * from sysobjects
  9769.         where name = 'spt_provider_types' and type = 'U '))
  9770.         begin
  9771.         drop table spt_provider_types
  9772.         dump tran master with no_log
  9773.         end
  9774.     end
  9775. go
  9776.  
  9777. if (charindex('7.00', @@version) = 0)
  9778. begin
  9779.     print ''
  9780.     print ''
  9781.     print 'Warning:'
  9782.     print 'you are installing the stored procedures '
  9783.     print 'on a pre 7.0 SQL Server.'
  9784.     print 'Ignore the following errors.'
  9785. end
  9786. go
  9787.  
  9788. /* 
  9789. ** (leih 2/11/98) 
  9790. ** The following SP is shared by instcat.sql and the server to add information
  9791. ** related to server language collations. The server calls it at the end of
  9792. ** collation change to update the catalog with the new language collations
  9793. ** Everything in this SP should NOT use tempdb.  
  9794. */
  9795. create procedure sp_add_server_sortinfo
  9796. as
  9797. -- spt_server_info has unigue clustered index on attribute_id
  9798. delete spt_server_info where attribute_id in (16,18)
  9799.  
  9800. --insert into spt_server_info
  9801. --    select 18,'COLLATION_SEQ',
  9802. --        'charset='+t2.name+' sort_order='+t1.name
  9803. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  9804. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  9805. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  9806. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  9807. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  9808. declare @case_sensitive bit
  9809. declare @ncase_sensitive bit
  9810. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  9811. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9812. select @sortid = value from sysconfigures where config = 1123
  9813. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  9814. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  9815.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  9816.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  9817.         from syscharsets where id = @csid
  9818.  
  9819. insert into spt_server_info
  9820.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  9821.  
  9822. if @case_sensitive = 1 /* If case sensitive server */
  9823. begin
  9824.     insert into spt_server_info
  9825.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  9826. end
  9827. else
  9828. begin
  9829.     insert into spt_server_info
  9830.         values (16,'IDENTIFIER_CASE','MIXED')
  9831. end
  9832.  
  9833.  
  9834. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  9835.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  9836. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  9837.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  9838. update spt_provider_types set case_sensitive = @case_sensitive 
  9839.     where oledb_data_type = 129 /*DBTYPE_STR*/
  9840. update spt_provider_types set case_sensitive = @ncase_sensitive
  9841.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  9842.  
  9843. go
  9844.  
  9845. if (charindex('7.00', @@version) <> 0)
  9846. begin
  9847.     exec sp_add_server_sortinfo
  9848. end
  9849. go
  9850.  
  9851. print ''
  9852. print 'creating sp_catalogs_rowset'
  9853. go
  9854.  
  9855. create procedure sp_catalogs_rowset
  9856.     (
  9857.     @catalog_name        varchar(255)
  9858.     )        
  9859. as
  9860.     select
  9861.         CATALOG_NAME    = name,
  9862.         DESCRIPTION    = convert(varchar(1),null)
  9863.     from     master.dbo.sysdatabases
  9864.     where    name = @catalog_name
  9865. go
  9866. dump tran master with no_log
  9867. go
  9868. create procedure sp_catalogs_rowset;2
  9869.     (
  9870.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9871.     )
  9872. as
  9873.     select
  9874.         CATALOG_NAME    = name,
  9875.         DESCRIPTION    = convert(varchar(1),null)
  9876.     from     master.dbo.sysdatabases
  9877.     order by 1
  9878. go
  9879. dump tran master with no_log
  9880. go
  9881.  
  9882. if (charindex('7.00', @@version) = 0)
  9883. begin
  9884.     print ''
  9885.     print ''
  9886.     print 'Warning:'
  9887.     print 'you are installing the stored procedures '
  9888.     print 'on a pre 7.0 SQL Server.'
  9889.     print 'Ignore the following errors.'
  9890. end
  9891. else
  9892.     drop proc sp_catalogs_rowset
  9893. go
  9894.  
  9895. /*    Procedure for 7.0 server */
  9896. create procedure sp_catalogs_rowset
  9897.     (
  9898.     @catalog_name        sysname
  9899.     )        
  9900. as
  9901.     select
  9902.         CATALOG_NAME    = name,
  9903.         DESCRIPTION    = convert(nvarchar(1),null)
  9904.     from     master.dbo.sysdatabases
  9905.     where    name = @catalog_name
  9906. go
  9907. dump tran master with no_log
  9908. go
  9909. create procedure sp_catalogs_rowset;2
  9910.     (
  9911.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9912.     )
  9913. as
  9914.     select
  9915.         CATALOG_NAME    = name,
  9916.         DESCRIPTION    = convert(nvarchar(1),null)
  9917.     from     master.dbo.sysdatabases
  9918.     order by 1
  9919. go
  9920. dump tran master with no_log
  9921. go
  9922. create procedure sp_catalogs_rowset;5
  9923.     (
  9924.     @server_name    sysname,
  9925.     @catalog_name    sysname = NULL
  9926.     )
  9927. as
  9928.     select    CATALOG_NAME,
  9929.         DESCRIPTION
  9930.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  9931.     order by 1
  9932. go
  9933.  
  9934. grant execute on sp_catalogs_rowset to public
  9935. go
  9936.  
  9937. dump tran master with no_log
  9938. go
  9939. if (charindex('6.00', @@version) > 0)
  9940.     begin
  9941.     if (exists (select * from sysobjects
  9942.             where name = 'sp_catalogs_rowset' and type = 'P '))
  9943.         begin
  9944.         drop procedure sp_catalogs_rowset
  9945.         dump tran master with no_log
  9946.         end
  9947.     end
  9948. go
  9949.  
  9950.  
  9951. print ''
  9952. print 'creating sp_column_privileges_rowset'
  9953. go
  9954.  
  9955. /*    Procedure for 6.0 and 6.5 server */
  9956. CREATE PROCEDURE sp_column_privileges_rowset
  9957.     (
  9958.     @table_name     varchar(255) = null,
  9959.     @table_schema    varchar(255) = null,
  9960.     @column_name    varchar(255) = null,
  9961.     @grantor    varchar(255) = null,
  9962.     @grantee    varchar(255) = null
  9963.     )
  9964. as
  9965. IF @table_name is not null
  9966.     BEGIN
  9967.     select
  9968.         GRANTOR        = user_name(p.grantor),
  9969.         GRANTEE        = user_name(u.uid),
  9970.         TABLE_CATALOG    = db_name(),
  9971.         TABLE_SCHEMA    = user_name(o.uid),
  9972.         TABLE_NAME    = o.name,
  9973.         COLUMN_NAME    = c.name,
  9974.         COLUMN_GUID    = convert(binary(16),null),
  9975.         COLUMN_PROPID    = convert(int,null),
  9976.         PRIVILEGE_TYPE    = convert(varchar(30),
  9977.                     case p.action
  9978.                     when 193 then 'SELECT'
  9979.                     when 195 then 'INSERT'
  9980.                     when 197 then 'UPDATE'
  9981.                     else 'REFERENCES'
  9982.                     end),
  9983.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9984.     from 
  9985.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9986.     where
  9987.         o.name = @table_name
  9988.     and     o.type in ('U','V','S')
  9989.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9990.     and    c.id = o.id
  9991.     and     (@column_name is null or @column_name = c.name)
  9992.     and     c.id = p.id
  9993.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9994.     and     case 
  9995.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9996.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9997.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9998.             end & v.high <> 0            /* permission applies to this column */
  9999.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10000.     and     v.type = 'P'
  10001.     and     v.number = c.colid
  10002.     and    (@grantee is null or @grantee = user_name(u.uid))
  10003.             /* expand groups */
  10004.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10005.     and     p.protecttype <> 206    /* only grant rows */
  10006.     and     p.action in (26,193,195,197)
  10007.     and     o.uid <> u.uid            /* no rows for owner */
  10008.     and     not exists (            /* exclude revoke'd privileges */
  10009.             select *
  10010.             from sysprotects p1
  10011.             where
  10012.                 p1.protecttype = 206
  10013.             and     p1.action = p.action
  10014.             and     p1.id = p.id
  10015.             and     p1.uid = u.uid
  10016.             and     case 
  10017.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10018.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10019.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10020.                 end & v.high <> 0)            /* permission applies to this column */
  10021.     union all
  10022.     select    /*    Add rows for table owner */
  10023.         GRANTOR        = user_name(u.uid),
  10024.         GRANTEE        = user_name(o.uid),
  10025.         TABLE_CATALOG    = db_name(),
  10026.         TABLE_SCHEMA    = user_name(o.uid),
  10027.         TABLE_NAME    = o.name,
  10028.         COLUMN_NAME    = c.name,
  10029.         COLUMN_GUID    = convert(binary(16),null),
  10030.         COLUMN_PROPID    = convert(int,null),
  10031.         PRIVILEGE_TYPE    = convert(varchar(30),
  10032.                     case v.number
  10033.                     when 193 then 'SELECT'
  10034.                     when 195 then 'INSERT'
  10035.                     when 197 then 'UPDATE'
  10036.                     else 'REFERENCES'
  10037.                     end),
  10038.         IS_GRANTABLE    = convert(bit,1)    
  10039.     from 
  10040.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10041.     where
  10042.         o.name = @table_name
  10043.     and     o.type in ('U','V','S')
  10044.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10045.     and    (@grantee is null or @grantee = user_name(o.uid))
  10046.     and    c.id = o.id
  10047.     and     (@column_name is null or @column_name = c.name)
  10048.     and     u.suid = 1        /* grantor is dbo of database */
  10049.     and    (@grantor is null or @grantor = user_name(u.uid))
  10050.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10051.     and     v.number in (26,193,195,197)
  10052.     and     not exists (        /* exclude revoke'd privileges */
  10053.             select *
  10054.             from     sysprotects p1
  10055.             where    p1.protecttype = 206
  10056.             and     p1.action = v.number
  10057.             and     p1.id = o.id
  10058.             and     p1.uid = o.uid)
  10059.     order by 4, 5, 6, 9, 1, 2
  10060.     END
  10061. ELSE
  10062.     BEGIN
  10063.     select
  10064.         GRANTOR        = user_name(p.grantor),
  10065.         GRANTEE        = user_name(u.uid),
  10066.         TABLE_CATALOG    = db_name(),
  10067.         TABLE_SCHEMA    = user_name(o.uid),
  10068.         TABLE_NAME    = o.name,
  10069.         COLUMN_NAME    = c.name,
  10070.         COLUMN_GUID    = convert(binary(16),null),
  10071.         COLUMN_PROPID    = convert(int,null),
  10072.         PRIVILEGE_TYPE    = convert(varchar(30),
  10073.                     case p.action
  10074.                     when 193 then 'SELECT'
  10075.                     when 195 then 'INSERT'
  10076.                     when 197 then 'UPDATE'
  10077.                     else 'REFERENCES'
  10078.                     end),
  10079.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10080.     from 
  10081.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10082.     where
  10083.         o.type in ('U','V','S')
  10084.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10085.     and    c.id = o.id
  10086.     and     (@column_name is null or @column_name = c.name)
  10087.     and     c.id = p.id
  10088.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10089.     and     case 
  10090.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10091.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10092.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10093.             end & v.high <> 0            /* permission applies to this column */
  10094.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10095.     and     v.type = 'P'
  10096.     and     v.number = c.colid
  10097.     and    (@grantee is null or @grantee = user_name(u.uid))
  10098.             /* expand groups */
  10099.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10100.     and     p.protecttype <> 206    /* only grant rows */
  10101.     and     p.action in (26,193,195,197)
  10102.     and     o.uid <> u.uid            /* no rows for owner */
  10103.     and     not exists (            /* exclude revoke'd privileges */
  10104.             select *
  10105.             from sysprotects p1
  10106.             where
  10107.                 p1.protecttype = 206
  10108.             and     p1.action = p.action
  10109.             and     p1.id = p.id
  10110.             and     p1.uid = u.uid
  10111.             and     case 
  10112.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10113.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10114.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10115.                 end & v.high <> 0)            /* permission applies to this column */
  10116.     union all
  10117.     select    /*    Add rows for table owner */
  10118.         GRANTOR        = user_name(u.uid),
  10119.         GRANTEE        = user_name(o.uid),
  10120.         TABLE_CATALOG    = db_name(),
  10121.         TABLE_SCHEMA    = user_name(o.uid),
  10122.         TABLE_NAME    = o.name,
  10123.         COLUMN_NAME    = c.name,
  10124.         COLUMN_GUID    = convert(binary(16),null),
  10125.         COLUMN_PROPID    = convert(int,null),
  10126.         PRIVILEGE_TYPE    = convert(varchar(30),
  10127.                     case v.number
  10128.                     when 193 then 'SELECT'
  10129.                     when 195 then 'INSERT'
  10130.                     when 197 then 'UPDATE'
  10131.                     else 'REFERENCES'
  10132.                     end),
  10133.         IS_GRANTABLE    = convert(bit,1)    
  10134.     from 
  10135.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10136.     where
  10137.          o.type in ('U','V','S')
  10138.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10139.     and    (@grantee is null or @grantee = user_name(o.uid))
  10140.     and    c.id = o.id
  10141.     and     (@column_name is null or @column_name = c.name)
  10142.     and     u.suid = 1        /* grantor is dbo of database */
  10143.     and    (@grantor is null or @grantor = user_name(u.uid))
  10144.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10145.     and     v.number in (26,193,195,197)
  10146.     and     not exists (        /* exclude revoke'd privileges */
  10147.             select *
  10148.             from     sysprotects p1
  10149.             where    p1.protecttype = 206
  10150.             and     p1.action = v.number
  10151.             and     p1.id = o.id
  10152.             and     p1.uid = o.uid)
  10153.     order by 4, 5, 6, 9, 1, 2
  10154.     END
  10155. go
  10156. dump tran master with no_log
  10157. go
  10158. CREATE PROCEDURE sp_column_privileges_rowset;2
  10159.     (
  10160.        @handle        int output,
  10161.        @scrollopt    int output,
  10162.     @ccopt        int output,
  10163.     @rows        int output,
  10164.     @table_name     varchar(255) = null,
  10165.     @table_schema    varchar(255) = null,
  10166.     @column_name    varchar(255) = null,
  10167.     @grantor    varchar(255) = null,
  10168.     @grantee    varchar(255) = null
  10169.     )
  10170. as
  10171.  
  10172. declare @ret int
  10173.  
  10174. SET NOCOUNT ON
  10175.     
  10176. create table #spcprivsrowset1
  10177.     (
  10178.     GRANTOR        sysname not null,
  10179.     GRANTEE        sysname not null,
  10180.     TABLE_CATALOG    sysname not null,
  10181.     TABLE_SCHEMA    sysname not null,
  10182.     TABLE_NAME    sysname not null,
  10183.     COLUMN_NAME    sysname not null,
  10184.     COLUMN_GUID    binary(16) null,
  10185.     COLUMN_PROPID    int null,
  10186.     PRIVILEGE_TYPE    sysname not null,
  10187.     IS_GRANTABLE    bit not null
  10188.     )
  10189.  
  10190. IF @table_name is not null
  10191.     BEGIN
  10192.     insert into #spcprivsrowset1
  10193.     select
  10194.         GRANTOR        = user_name(p.grantor),
  10195.         GRANTEE        = user_name(u.uid),
  10196.         TABLE_CATALOG    = db_name(),
  10197.         TABLE_SCHEMA    = user_name(o.uid),
  10198.         TABLE_NAME    = o.name,
  10199.         COLUMN_NAME    = c.name,
  10200.         COLUMN_GUID    = convert(binary(16),null),
  10201.         COLUMN_PROPID    = convert(int,null),
  10202.         PRIVILEGE_TYPE    = convert(varchar(30),
  10203.                     case p.action
  10204.                     when 193 then 'SELECT'
  10205.                     when 195 then 'INSERT'
  10206.                     when 197 then 'UPDATE'
  10207.                     else 'REFERENCES'
  10208.                     end),
  10209.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10210.     from 
  10211.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10212.     where
  10213.         o.name = @table_name
  10214.     and     o.type in ('U','V','S')
  10215.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10216.     and    c.id = o.id
  10217.     and     (@column_name is null or @column_name = c.name)
  10218.     and     c.id = p.id
  10219.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10220.     and     case 
  10221.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10222.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10223.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10224.             end & v.high <> 0            /* permission applies to this column */
  10225.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10226.     and     v.type = 'P'
  10227.     and     v.number = c.colid
  10228.     and    (@grantee is null or @grantee = user_name(u.uid))
  10229.             /* expand groups */
  10230.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10231.     and     p.protecttype <> 206    /* only grant rows */
  10232.     and     p.action in (26,193,195,197)
  10233.     and     o.uid <> u.uid            /* no rows for owner */
  10234.     and     not exists (            /* exclude revoke'd privileges */
  10235.             select *
  10236.             from sysprotects p1
  10237.             where
  10238.                 p1.protecttype = 206
  10239.             and     p1.action = p.action
  10240.             and     p1.id = p.id
  10241.             and     p1.uid = u.uid
  10242.             and     case 
  10243.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10244.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10245.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10246.                 end & v.high <> 0)            /* permission applies to this column */
  10247.     union all
  10248.     select    /*    Add rows for table owner */
  10249.         GRANTOR        = user_name(u.uid),
  10250.         GRANTEE        = user_name(o.uid),
  10251.         TABLE_CATALOG    = db_name(),
  10252.         TABLE_SCHEMA    = user_name(o.uid),
  10253.         TABLE_NAME    = o.name,
  10254.         COLUMN_NAME    = c.name,
  10255.         COLUMN_GUID    = convert(binary(16),null),
  10256.         COLUMN_PROPID    = convert(int,null),
  10257.         PRIVILEGE_TYPE    = convert(varchar(30),
  10258.                     case v.number
  10259.                     when 193 then 'SELECT'
  10260.                     when 195 then 'INSERT'
  10261.                     when 197 then 'UPDATE'
  10262.                     else 'REFERENCES'
  10263.                     end),
  10264.         IS_GRANTABLE    = convert(bit,1)    
  10265.     from 
  10266.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10267.     where
  10268.         o.name = @table_name
  10269.     and     o.type in ('U','V','S')
  10270.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10271.     and    (@grantee is null or @grantee = user_name(o.uid))
  10272.     and    c.id = o.id
  10273.     and     (@column_name is null or @column_name = c.name)
  10274.     and     u.suid = 1        /* grantor is dbo of database */
  10275.     and    (@grantor is null or @grantor = user_name(u.uid))
  10276.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10277.     and     v.number in (26,193,195,197)
  10278.     and     not exists (        /* exclude revoke'd privileges */
  10279.             select *
  10280.             from     sysprotects p1
  10281.             where    p1.protecttype = 206
  10282.             and     p1.action = v.number
  10283.             and     p1.id = o.id
  10284.             and     p1.uid = o.uid)
  10285.     order by 4, 5, 6, 9, 1, 2
  10286.     END
  10287. ELSE
  10288.     BEGIN
  10289.     insert into #spcprivsrowset1
  10290.     select
  10291.         GRANTOR        = user_name(p.grantor),
  10292.         GRANTEE        = user_name(u.uid),
  10293.         TABLE_CATALOG    = db_name(),
  10294.         TABLE_SCHEMA    = user_name(o.uid),
  10295.         TABLE_NAME    = o.name,
  10296.         COLUMN_NAME    = c.name,
  10297.         COLUMN_GUID    = convert(binary(16),null),
  10298.         COLUMN_PROPID    = convert(int,null),
  10299.         PRIVILEGE_TYPE    = convert(varchar(30),
  10300.                     case p.action
  10301.                     when 193 then 'SELECT'
  10302.                     when 195 then 'INSERT'
  10303.                     when 197 then 'UPDATE'
  10304.                     else 'REFERENCES'
  10305.                     end),
  10306.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10307.     from 
  10308.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  10309.     where
  10310.         o.type in ('U','V','S')
  10311.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10312.     and    c.id = o.id
  10313.     and     (@column_name is null or @column_name = c.name)
  10314.     and     c.id = p.id
  10315.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10316.     and     case 
  10317.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10318.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10319.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10320.             end & v.high <> 0            /* permission applies to this column */
  10321.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10322.     and     v.type = 'P'
  10323.     and     v.number = c.colid
  10324.     and    (@grantee is null or @grantee = user_name(u.uid))
  10325.             /* expand groups */
  10326.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  10327.     and     p.protecttype <> 206    /* only grant rows */
  10328.     and     p.action in (26,193,195,197)
  10329.     and     o.uid <> u.uid            /* no rows for owner */
  10330.     and     not exists (            /* exclude revoke'd privileges */
  10331.             select *
  10332.             from sysprotects p1
  10333.             where
  10334.                 p1.protecttype = 206
  10335.             and     p1.action = p.action
  10336.             and     p1.id = p.id
  10337.             and     p1.uid = u.uid
  10338.             and     case 
  10339.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10340.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10341.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10342.                 end & v.high <> 0)            /* permission applies to this column */
  10343.     union all
  10344.     select    /*    Add rows for table owner */
  10345.         GRANTOR        = user_name(u.uid),
  10346.         GRANTEE        = user_name(o.uid),
  10347.         TABLE_CATALOG    = db_name(),
  10348.         TABLE_SCHEMA    = user_name(o.uid),
  10349.         TABLE_NAME    = o.name,
  10350.         COLUMN_NAME    = c.name,
  10351.         COLUMN_GUID    = convert(binary(16),null),
  10352.         COLUMN_PROPID    = convert(int,null),
  10353.         PRIVILEGE_TYPE    = convert(varchar(30),
  10354.                     case v.number
  10355.                     when 193 then 'SELECT'
  10356.                     when 195 then 'INSERT'
  10357.                     when 197 then 'UPDATE'
  10358.                     else 'REFERENCES'
  10359.                     end),
  10360.         IS_GRANTABLE    = convert(bit,1)    
  10361.     from 
  10362.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10363.     where
  10364.          o.type in ('U','V','S')
  10365.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10366.     and    (@grantee is null or @grantee = user_name(o.uid))
  10367.     and    c.id = o.id
  10368.     and     (@column_name is null or @column_name = c.name)
  10369.     and     u.suid = 1        /* grantor is dbo of database */
  10370.     and    (@grantor is null or @grantor = user_name(u.uid))
  10371.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10372.     and     v.number in (26,193,195,197)
  10373.     and     not exists (        /* exclude revoke'd privileges */
  10374.             select *
  10375.             from     sysprotects p1
  10376.             where    p1.protecttype = 206
  10377.             and     p1.action = v.number
  10378.             and     p1.id = o.id
  10379.             and     p1.uid = o.uid)
  10380.     order by 4, 5, 6, 9, 1, 2
  10381.     END
  10382.  
  10383. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  10384.     @scrollopt output, @ccopt output, @rows output
  10385.  
  10386. drop table #spcprivsrowset1
  10387.  
  10388. return isnull(@ret,0)
  10389. go
  10390. dump tran master with no_log
  10391. go
  10392. CREATE PROCEDURE sp_column_privileges_rowset;3
  10393. as
  10394.     select
  10395.         GRANTOR        = convert(sysname,' '),
  10396.         GRANTEE        = convert(sysname,' '),
  10397.         TABLE_CATALOG    = convert(sysname,' '),
  10398.         TABLE_SCHEMA    = convert(sysname,' '),
  10399.         TABLE_NAME    = convert(sysname,' '),
  10400.         COLUMN_NAME    = convert(sysname,' '),
  10401.         COLUMN_GUID    = convert(binary(16),null),
  10402.         COLUMN_PROPID    = convert(int,null),
  10403.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  10404.         IS_GRANTABLE    = convert(bit,1)
  10405.     where    1=0
  10406. go
  10407. dump tran master with no_log
  10408. go
  10409.  
  10410. if (charindex('7.00', @@version) = 0)
  10411. begin
  10412.     print ''
  10413.     print ''
  10414.     print 'Warning:'
  10415.     print 'you are installing the stored procedures '
  10416.     print 'on a pre 7.0 SQL Server.'
  10417.     print 'Ignore the following errors.'
  10418. end
  10419. else
  10420.     drop proc sp_column_privileges_rowset
  10421. go
  10422.  
  10423.  
  10424. /*    Procedure for 7.0 server */
  10425. CREATE PROCEDURE sp_column_privileges_rowset
  10426.     (
  10427.     @table_name         sysname,
  10428.     @table_schema        sysname = null,
  10429.     @column_name        sysname = null,
  10430.     @grantor        sysname = null,
  10431.     @grantee        sysname = null
  10432.     )
  10433. as
  10434.     select
  10435.         GRANTOR        = user_name(p.grantor),
  10436.         GRANTEE        = user_name(u.uid),
  10437.         TABLE_CATALOG    = db_name(),
  10438.         TABLE_SCHEMA    = user_name(o.uid),
  10439.         TABLE_NAME    = o.name,
  10440.         COLUMN_NAME    = c.name,
  10441.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10442.         COLUMN_PROPID    = convert(int,null),
  10443.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10444.                     case p.action
  10445.                     when 193 then N'SELECT'
  10446.                     when 195 then N'INSERT'
  10447.                     when 197 then N'UPDATE'
  10448.                     else N'REFERENCES'
  10449.                     end),
  10450.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10451.     from 
  10452.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10453.     where
  10454.         o.name = @table_name
  10455.     and     o.type in ('U','V','S')
  10456.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10457.     and    c.id = o.id
  10458.     and     (@column_name is null or @column_name = c.name)
  10459.     and     c.id = p.id
  10460.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10461.     and     case 
  10462.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10463.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10464.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10465.             end & v.high <> 0            /* permission applies to this column */
  10466.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10467.     and     v.type = 'P'
  10468.     and     v.number = c.colid
  10469.     and    (@grantee is null or @grantee = user_name(u.uid))
  10470.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10471.     and     (u.uid > 0 and u.uid < 16384)
  10472.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10473.     and     p.protecttype <> 206    /* only grant rows */
  10474.     and     p.action in (26,193,195,197)
  10475.     and     o.uid <> u.uid            /* no rows for owner */
  10476.     and     not exists (            /* exclude revoke'd privileges */
  10477.             select *
  10478.             from sysprotects p1
  10479.             where
  10480.                 p1.protecttype = 206
  10481.             and     p1.action = p.action
  10482.             and     p1.id = p.id
  10483.             and     p1.uid = u.uid
  10484.             and     case 
  10485.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10486.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10487.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10488.                 end & v.high <> 0)            /* permission applies to this column */
  10489.     union all
  10490.     select    /*    Add rows for table owner */
  10491.         GRANTOR        = user_name(u.uid),
  10492.         GRANTEE        = user_name(o.uid),
  10493.         TABLE_CATALOG    = db_name(),
  10494.         TABLE_SCHEMA    = user_name(o.uid),
  10495.         TABLE_NAME    = o.name,
  10496.         COLUMN_NAME    = c.name,
  10497.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10498.         COLUMN_PROPID    = convert(int,null),
  10499.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10500.                     case v.number
  10501.                     when 193 then N'SELECT'
  10502.                     when 195 then N'INSERT'
  10503.                     when 197 then N'UPDATE'
  10504.                     else N'REFERENCES'
  10505.                     end),
  10506.         IS_GRANTABLE    = convert(bit,1)    
  10507.     from 
  10508.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10509.     where
  10510.         o.name = @table_name
  10511.     and     o.type in ('U','V','S')
  10512.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10513.     and    (@grantee is null or @grantee = user_name(o.uid))
  10514.     and    c.id = o.id
  10515.     and     (@column_name is null or @column_name = c.name)
  10516.     and     u.suid = 1        /* grantor is dbo of database */
  10517.     and    (@grantor is null or @grantor = user_name(u.uid))
  10518.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10519.     and     v.number in (26,193,195,197)
  10520.     and     not exists (        /* exclude revoke'd privileges */
  10521.             select *
  10522.             from     sysprotects p1
  10523.             where    p1.protecttype = 206
  10524.             and     p1.action = v.number
  10525.             and     p1.id = o.id
  10526.             and     p1.uid = o.uid)
  10527.     order by 4, 5, 6, 9, 1, 2
  10528. go
  10529. dump tran master with no_log
  10530. go
  10531. CREATE PROCEDURE sp_column_privileges_rowset;2
  10532.     (
  10533.     @table_schema        sysname = null,
  10534.     @column_name        sysname = null,
  10535.     @grantor        sysname = null,
  10536.     @grantee        sysname = null
  10537.     )
  10538. as
  10539.     select
  10540.         GRANTOR        = user_name(p.grantor),
  10541.         GRANTEE        = user_name(u.uid),
  10542.         TABLE_CATALOG    = db_name(),
  10543.         TABLE_SCHEMA    = user_name(o.uid),
  10544.         TABLE_NAME    = o.name,
  10545.         COLUMN_NAME    = c.name,
  10546.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10547.         COLUMN_PROPID    = convert(int,null),
  10548.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10549.                     case p.action
  10550.                     when 193 then N'SELECT'
  10551.                     when 195 then N'INSERT'
  10552.                     when 197 then N'UPDATE'
  10553.                     else N'REFERENCES'
  10554.                     end),
  10555.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10556.     from 
  10557.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10558.     where
  10559.         o.type in ('U','V','S')
  10560.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10561.     and    c.id = o.id
  10562.     and     (@column_name is null or @column_name = c.name)
  10563.     and     c.id = p.id
  10564.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10565.     and     case 
  10566.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10567.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10568.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10569.             end & v.high <> 0            /* permission applies to this column */
  10570.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10571.     and     v.type = 'P'
  10572.     and     v.number = c.colid
  10573.     and    (@grantee is null or @grantee = user_name(u.uid))
  10574.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10575.     and     (u.uid > 0 and u.uid < 16384)
  10576.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10577.     and     p.protecttype <> 206    /* only grant rows */
  10578.     and     p.action in (26,193,195,197)
  10579.     and     o.uid <> u.uid            /* no rows for owner */
  10580.     and     not exists (            /* exclude revoke'd privileges */
  10581.             select *
  10582.             from sysprotects p1
  10583.             where
  10584.                 p1.protecttype = 206
  10585.             and     p1.action = p.action
  10586.             and     p1.id = p.id
  10587.             and     p1.uid = u.uid
  10588.             and     case 
  10589.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10590.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10591.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10592.                 end & v.high <> 0)            /* permission applies to this column */
  10593.     union all
  10594.     select    /*    Add rows for table owner */
  10595.         GRANTOR        = user_name(u.uid),
  10596.         GRANTEE        = user_name(o.uid),
  10597.         TABLE_CATALOG    = db_name(),
  10598.         TABLE_SCHEMA    = user_name(o.uid),
  10599.         TABLE_NAME    = o.name,
  10600.         COLUMN_NAME    = c.name,
  10601.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10602.         COLUMN_PROPID    = convert(int,null),
  10603.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10604.                     case v.number
  10605.                     when 193 then N'SELECT'
  10606.                     when 195 then N'INSERT'
  10607.                     when 197 then N'UPDATE'
  10608.                     else N'REFERENCES'
  10609.                     end),
  10610.         IS_GRANTABLE    = convert(bit,1)    
  10611.     from 
  10612.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10613.     where
  10614.         o.type in ('U','V','S')
  10615.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10616.     and    (@grantee is null or @grantee = user_name(o.uid))
  10617.     and    c.id = o.id
  10618.     and     (@column_name is null or @column_name = c.name)
  10619.     and     u.suid = 1        /* grantor is dbo of database */
  10620.     and    (@grantor is null or @grantor = user_name(u.uid))
  10621.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10622.     and     v.number in (26,193,195,197)
  10623.     and     not exists (        /* exclude revoke'd privileges */
  10624.             select *
  10625.             from     sysprotects p1
  10626.             where    p1.protecttype = 206
  10627.             and     p1.action = v.number
  10628.             and     p1.id = o.id
  10629.             and     p1.uid = o.uid)
  10630.     order by 4, 5, 6, 9, 1, 2
  10631. go
  10632. dump tran master with no_log
  10633. go
  10634. create procedure sp_column_privileges_rowset;5
  10635.     (
  10636.     @table_server        sysname,
  10637.     @table_catalog        sysname = null,
  10638.     @table_name        sysname = null,
  10639.     @table_schema        sysname = null,
  10640.     @column_name        sysname = null,
  10641.     @grantor        sysname = null,
  10642.     @grantee        sysname = null
  10643.     )
  10644. as
  10645.     select
  10646.         GRANTOR,
  10647.         GRANTEE,
  10648.         TABLE_CATALOG,
  10649.         TABLE_SCHEMA,
  10650.         TABLE_NAME,
  10651.         COLUMN_NAME,
  10652.         COLUMN_GUID,
  10653.         COLUMN_PROPID,    
  10654.         PRIVILEGE_TYPE,
  10655.         IS_GRANTABLE    
  10656.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  10657.                 @table_server,
  10658.                 @table_catalog,
  10659.                 @table_schema,
  10660.                 @table_name,
  10661.                 @column_name,
  10662.                 @grantor,
  10663.                 @grantee>
  10664.     order by 3, 4, 5, 6, 9, 1, 2
  10665. go
  10666.  
  10667. grant execute on sp_column_privileges_rowset to public
  10668. go
  10669.  
  10670. dump tran master with no_log
  10671. go
  10672. if (charindex('6.00', @@version) > 0)
  10673.     begin
  10674.     if (exists (select * from sysobjects
  10675.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  10676.         begin
  10677.         drop procedure sp_column_privileges_rowset
  10678.         dump tran master with no_log
  10679.         end
  10680.     end
  10681. go
  10682.  
  10683.  
  10684. print ''
  10685. print 'creating sp_columns_rowset'
  10686. go
  10687.  
  10688. /*    Procedure for 6.0 and 6.50 server */
  10689. create procedure sp_columns_rowset
  10690.     (
  10691.        @table_name    varchar(255),
  10692.     @table_schema     varchar(255) = null,
  10693.     @column_name    varchar(255) = null
  10694.     )
  10695. as     
  10696.     set nocount on
  10697.  
  10698.     /* Declare bindings */
  10699.     DECLARE @TABLECATALOG    sysname,
  10700.         @TABLESCHEMA     sysname,
  10701.         @TABLENAME       sysname,
  10702.         @COLUMNNAME      sysname,
  10703.         @ORDINALPOSITION int
  10704.  
  10705.     /* Declare tracking variables */
  10706.     DECLARE @currCatalog sysname,
  10707.          @currSchema  sysname,
  10708.         @currName    sysname,
  10709.         @currPos     int
  10710.  
  10711.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  10712.     CREATE TABLE #columns_rowset
  10713.     (
  10714.         TABLE_CATALOG            sysname,
  10715.         TABLE_SCHEMA            sysname,
  10716.         TABLE_NAME            sysname,
  10717.         COLUMN_NAME            sysname,
  10718.         COLUMN_GUID            binary(16)        null,
  10719.         COLUMN_PROPID            int            null,
  10720.         ORDINAL_POSITION        int,
  10721.         COLUMN_HASDEFAULT        bit,
  10722.         COLUMN_DEFAULT            varchar(255),
  10723.         COLUMN_FLAGS            int,
  10724.         IS_NULLABLE            bit,
  10725.         DATA_TYPE            smallint,
  10726.         TYPE_GUID            binary(16)        null,
  10727.         CHARACTER_MAXIMUM_LENGTH    int            null,
  10728.         CHARACTER_OCTET_LENGTH        int            null,
  10729.         NUMERIC_PRECISION        smallint        null,
  10730.         NUMERIC_SCALE            smallint        null, 
  10731.         DATETIME_PRECISION        int            null,
  10732.         CHARACTER_SET_CATALOG        sysname            null,
  10733.         CHARACTER_SET_SCHEMA        sysname            null,
  10734.         CHARACTER_SET_NAME        sysname            null,
  10735.         COLLATION_CATALOG        sysname            null,
  10736.         COLLATION_SCHEMA        sysname            null,
  10737.         COLLATION_NAME            sysname            null,
  10738.         DOMAIN_CATALOG            sysname         null,
  10739.         DOMAIN_SCHEMA            sysname         null,
  10740.         DOMAIN_NAME            sysname         null,
  10741.         DESCRIPTION            varchar(1)
  10742.     )        
  10743.  
  10744.     /* Declare cursor for fixup */
  10745.     DECLARE curColRowset CURSOR
  10746.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  10747.         FROM #columns_rowset
  10748.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  10749.  
  10750.  
  10751.     /* Store results into the temp table */
  10752.     insert into #columns_rowset
  10753.         (
  10754.         TABLE_CATALOG,
  10755.         TABLE_SCHEMA,
  10756.         TABLE_NAME,
  10757.         COLUMN_NAME,
  10758.         COLUMN_GUID,
  10759.         COLUMN_PROPID,
  10760.         ORDINAL_POSITION,
  10761.         COLUMN_HASDEFAULT,
  10762.         COLUMN_DEFAULT,
  10763.         COLUMN_FLAGS,
  10764.         IS_NULLABLE,
  10765.         DATA_TYPE,
  10766.         TYPE_GUID,
  10767.         CHARACTER_MAXIMUM_LENGTH,
  10768.         CHARACTER_OCTET_LENGTH,
  10769.         NUMERIC_PRECISION,
  10770.         NUMERIC_SCALE,
  10771.         DATETIME_PRECISION,
  10772.         CHARACTER_SET_CATALOG,
  10773.         CHARACTER_SET_SCHEMA,
  10774.         CHARACTER_SET_NAME,
  10775.         COLLATION_CATALOG,
  10776.         COLLATION_SCHEMA,
  10777.         COLLATION_NAME,
  10778.         DOMAIN_CATALOG,
  10779.         DOMAIN_SCHEMA,
  10780.         DOMAIN_NAME,
  10781.         DESCRIPTION
  10782.         )
  10783.     select
  10784.         TABLE_CATALOG        = db_name(),
  10785.         TABLE_SCHEMA        = user_name(o.uid),
  10786.         TABLE_NAME        = o.name,
  10787.         COLUMN_NAME        = c.name,
  10788.         COLUMN_GUID        = convert(binary(16),null),
  10789.         COLUMN_PROPID        = convert(int,null),
  10790.         ORDINAL_POSITION    = convert(int, c.colid),
  10791.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10792.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10793.         COLUMN_FLAGS        = convert(int,
  10794.                         case when d.is_long = 1 
  10795.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10796.                         else 0
  10797.                         end
  10798.                     |    case when d.fixlen is not null
  10799.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10800.                         else 0
  10801.                         end
  10802.                     |    case when c.status&8 = 8
  10803.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10804.                         else 0
  10805.                         end
  10806.                     |    case 
  10807.                         when d.type_name = 'timestamp'
  10808.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10809.                         when (c.status&128) != 128
  10810.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10811.                         else 0 
  10812.                         end),
  10813.         IS_NULLABLE        = convert(bit,c.status&8),
  10814.         DATA_TYPE        = d.oledb_data_type,
  10815.         TYPE_GUID        = convert(binary(16),null),
  10816.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10817.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10818.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10819.                         then coalesce(d.column_size,c.length)
  10820.                         else null 
  10821.                         end),
  10822.         CHARACTER_OCTET_LENGTH    = convert(int,
  10823.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10824.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10825.                         then coalesce(d.column_size,c.length)
  10826.                         else null 
  10827.                         end),
  10828.         NUMERIC_PRECISION    = convert(smallint,
  10829.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10830.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10831.         NUMERIC_SCALE        = convert(smallint, 
  10832.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10833.         DATETIME_PRECISION    = convert(int,
  10834.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10835.                             when data_precision = 23 then 3 else 0 end),
  10836.         CHARACTER_SET_CATALOG    = convert(sysname,
  10837.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10838.                         then 'master' 
  10839.                         else null 
  10840.                         end),
  10841.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10842.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10843.                         then 'dbo' 
  10844.                         else null 
  10845.                         end),
  10846.         CHARACTER_SET_NAME    = convert(sysname,
  10847.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10848.                         then a_cha.name 
  10849.                         else null 
  10850.                         end),
  10851.         COLLATION_CATALOG    = convert(sysname,
  10852.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10853.                         then 'master' 
  10854.                         else null 
  10855.                         end),
  10856.         COLLATION_SCHEMA    = convert(sysname,
  10857.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10858.                         then 'dbo' 
  10859.                         else null 
  10860.                         end),
  10861.         COLLATION_NAME        = convert(sysname,
  10862.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10863.                         then b_cha.name 
  10864.                         else null 
  10865.                         end),
  10866.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10867.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10868.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10869.         DESCRIPTION        = convert(varchar(1),null)    
  10870.     
  10871.     from
  10872.         syscolumns c,
  10873.         syscomments m,
  10874.         sysobjects o,
  10875.         master.dbo.spt_provider_types d,
  10876.         systypes t,
  10877.         sysusers u,
  10878.         master.dbo.sysconfigures    cfg,
  10879.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10880.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  10881.     where
  10882.         o.name = @table_name
  10883.     and     o.type in ('U','V','S')
  10884.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10885.     and     (
  10886.         suser_id() = 1     /* User is the System Administrator */
  10887.         or o.uid = user_id()     /* User created the object */
  10888.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10889.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10890.             from sysprotects p
  10891.             where p.id = o.id
  10892.             /* get rows for public,current user,user's group */
  10893.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10894.             /* check for SELECT,EXECUTE privilege */
  10895.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10896.             ) = 1    /* final magic...compare Grants      */
  10897.            )
  10898.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10899.     and    (@column_name is null or @column_name = c.name)
  10900.     and     o.id = c.id
  10901.     and     t.type = d.ss_dtype
  10902.     and    (t.usertype != 80 or d.type_name='timestamp')
  10903.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10904.     and     c.usertype = t.usertype
  10905.     and    c.cdefault *= m.id
  10906.     and    m.colid = 1
  10907.     and    cfg.comment = 'default sortorder ID' 
  10908.     and    a_cha.type = 1001 /* type is charset */
  10909.     and    b_cha.type = 2001 /* type is sortorder */
  10910.     and    a_cha.id = b_cha.csid
  10911.     and     b_cha.id = cfg.value
  10912.     order by 2, 3, c.colid
  10913.  
  10914.     /* Fix ordinal positions */
  10915.     OPEN curColRowset
  10916.     FETCH NEXT FROM curColRowset
  10917.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  10918.     
  10919.     /* Initialize tracking variables */
  10920.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  10921.  
  10922.     WHILE @@FETCH_STATUS = 0
  10923.     BEGIN
  10924.         /* Check to see if the catalog/schema/table */
  10925.         /* has changed since last row               */
  10926.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  10927.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  10928.         BEGIN
  10929.             /* Increment ordinal count */
  10930.             SELECT @currPos = @currPos + 1
  10931.         END
  10932.         ELSE
  10933.         BEGIN
  10934.             /* Set the catalog/schema/table to new values */
  10935.             SELECT @currCatalog = @TABLECATALOG,
  10936.                    @currSchema  = @TABLESCHEMA,
  10937.                    @currName    = @TABLENAME,
  10938.                    @currPos     = 1
  10939.         END
  10940.  
  10941.         /* Check for holes in the ordinal position order */
  10942.         if (@currPos < @ORDINALPOSITION)
  10943.         BEGIN        
  10944.             /* Update the ordinal position */
  10945.             UPDATE #columns_rowset
  10946.             SET    ORDINAL_POSITION = @currPos
  10947.             WHERE  TABLE_CATALOG = @TABLECATALOG
  10948.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  10949.               AND  TABLE_NAME    = @TABLENAME
  10950.               AND  COLUMN_NAME   = @COLUMNNAME
  10951.         END
  10952.  
  10953.         /* Fetch next result */
  10954.         FETCH NEXT FROM curColRowset
  10955.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  10956.     END
  10957.  
  10958.     /* Cleanup cursor */
  10959.     CLOSE curColRowset
  10960.     DEALLOCATE curColRowset
  10961.  
  10962.     set nocount off
  10963.  
  10964.     /* Output the result set */
  10965.     select *
  10966.     from #columns_rowset
  10967.     order by 2, 3, 7
  10968. go
  10969.  
  10970. dump tran master with no_log
  10971. go
  10972.  
  10973. create procedure sp_columns_rowset;2
  10974.     (
  10975.     @table_schema     varchar(255) = null,
  10976.     @column_name    varchar(255) = null
  10977.     )
  10978. as     
  10979.     set nocount on
  10980.  
  10981.     /* Declare bindings */
  10982.     DECLARE @TABLECATALOG    sysname,
  10983.         @TABLESCHEMA     sysname,
  10984.         @TABLENAME       sysname,
  10985.         @COLUMNNAME      sysname,
  10986.         @ORDINALPOSITION int
  10987.  
  10988.     /* Declare tracking variables */
  10989.     DECLARE @currCatalog sysname,
  10990.          @currSchema  sysname,
  10991.         @currName    sysname,
  10992.         @currPos     int
  10993.  
  10994.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  10995.     CREATE TABLE #columns_rowset
  10996.     (
  10997.         TABLE_CATALOG            sysname,
  10998.         TABLE_SCHEMA            sysname,
  10999.         TABLE_NAME            sysname,
  11000.         COLUMN_NAME            sysname,
  11001.         COLUMN_GUID            binary(16)        null,
  11002.         COLUMN_PROPID            int            null,
  11003.         ORDINAL_POSITION        int            null,
  11004.         COLUMN_HASDEFAULT        bit,
  11005.         COLUMN_DEFAULT            varchar(255),
  11006.         COLUMN_FLAGS            int,
  11007.         IS_NULLABLE            bit,
  11008.         DATA_TYPE            smallint,
  11009.         TYPE_GUID            binary(16)        null,
  11010.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11011.         CHARACTER_OCTET_LENGTH        int            null,
  11012.         NUMERIC_PRECISION        smallint        null,
  11013.         NUMERIC_SCALE            smallint        null, 
  11014.         DATETIME_PRECISION        int            null,
  11015.         CHARACTER_SET_CATALOG        sysname            null,
  11016.         CHARACTER_SET_SCHEMA        sysname            null,
  11017.         CHARACTER_SET_NAME        sysname            null,
  11018.         COLLATION_CATALOG        sysname            null,
  11019.         COLLATION_SCHEMA        sysname            null,
  11020.         COLLATION_NAME            sysname            null,
  11021.         DOMAIN_CATALOG            sysname         null,
  11022.         DOMAIN_SCHEMA            sysname         null,
  11023.         DOMAIN_NAME            sysname         null,
  11024.         DESCRIPTION            varchar(1)
  11025.     )        
  11026.  
  11027.     /* Declare cursor for fixup */
  11028.     DECLARE curColRowset CURSOR
  11029.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  11030.         FROM #columns_rowset
  11031.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  11032.  
  11033.     /* Store results into the temp table */
  11034.     insert into #columns_rowset
  11035.         (
  11036.         TABLE_CATALOG,
  11037.         TABLE_SCHEMA,
  11038.         TABLE_NAME,
  11039.         COLUMN_NAME,
  11040.         COLUMN_GUID,
  11041.         COLUMN_PROPID,
  11042.         ORDINAL_POSITION,
  11043.         COLUMN_HASDEFAULT,
  11044.         COLUMN_DEFAULT,
  11045.         COLUMN_FLAGS,
  11046.         IS_NULLABLE,
  11047.         DATA_TYPE,
  11048.         TYPE_GUID,
  11049.         CHARACTER_MAXIMUM_LENGTH,
  11050.         CHARACTER_OCTET_LENGTH,
  11051.         NUMERIC_PRECISION,
  11052.         NUMERIC_SCALE,
  11053.         DATETIME_PRECISION,
  11054.         CHARACTER_SET_CATALOG,
  11055.         CHARACTER_SET_SCHEMA,
  11056.         CHARACTER_SET_NAME,
  11057.         COLLATION_CATALOG,
  11058.         COLLATION_SCHEMA,
  11059.         COLLATION_NAME,
  11060.         DOMAIN_CATALOG,
  11061.         DOMAIN_SCHEMA,
  11062.         DOMAIN_NAME,
  11063.         DESCRIPTION
  11064.         )
  11065.     select
  11066.         TABLE_CATALOG        = db_name(),
  11067.         TABLE_SCHEMA        = user_name(o.uid),
  11068.         TABLE_NAME        = o.name,
  11069.         COLUMN_NAME        = c.name,
  11070.         COLUMN_GUID        = convert(binary(16),null),
  11071.         COLUMN_PROPID        = convert(int,null),
  11072.         ORDINAL_POSITION    = convert(int, c.colid),
  11073.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11074.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  11075.         COLUMN_FLAGS        = convert(int,
  11076.                         case when d.is_long = 1 
  11077.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11078.                         else 0
  11079.                         end
  11080.                     |    case when d.fixlen is not null
  11081.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11082.                         else 0
  11083.                         end
  11084.                     |    case when c.status&8 = 8
  11085.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11086.                         else 0
  11087.                         end
  11088.                     |    case 
  11089.                         when d.type_name = 'timestamp'
  11090.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11091.                         when (c.status&128) != 128
  11092.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  11093.                         else 0 
  11094.                         end),
  11095.         IS_NULLABLE        = convert(bit,c.status&8),
  11096.         DATA_TYPE        = d.oledb_data_type,
  11097.         TYPE_GUID        = convert(binary(16),null),
  11098.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11099.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11100.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11101.                         then coalesce(d.column_size,c.length)
  11102.                         else null 
  11103.                         end),
  11104.         CHARACTER_OCTET_LENGTH    = convert(int,
  11105.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11106.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11107.                         then coalesce(d.column_size,c.length)
  11108.                         else null 
  11109.                         end),
  11110.         NUMERIC_PRECISION    = convert(smallint,
  11111.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  11112.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11113.         NUMERIC_SCALE        = convert(smallint, 
  11114.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11115.         DATETIME_PRECISION    = convert(int,
  11116.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11117.                             when data_precision = 23 then 3 else 0 end),
  11118.         CHARACTER_SET_CATALOG    = convert(sysname,
  11119.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11120.                         then 'master' 
  11121.                         else null 
  11122.                         end),
  11123.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11124.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11125.                         then 'dbo' 
  11126.                         else null 
  11127.                         end),
  11128.         CHARACTER_SET_NAME    = convert(sysname,
  11129.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11130.                         then a_cha.name 
  11131.                         else null 
  11132.                         end),
  11133.         COLLATION_CATALOG    = convert(sysname,
  11134.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11135.                         then 'master' 
  11136.                         else null 
  11137.                         end),
  11138.         COLLATION_SCHEMA    = convert(sysname,
  11139.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11140.                         then 'dbo' 
  11141.                         else null 
  11142.                         end),
  11143.         COLLATION_NAME        = convert(sysname,
  11144.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11145.                         then b_cha.name 
  11146.                         else null 
  11147.                         end),
  11148.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11149.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11150.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11151.         DESCRIPTION        = convert(varchar(1),null)    
  11152.     
  11153.     from
  11154.         syscolumns c,
  11155.         syscomments m,
  11156.         sysobjects o,
  11157.         master.dbo.spt_provider_types d,
  11158.         systypes t,
  11159.         sysusers u,
  11160.         master.dbo.sysconfigures    cfg,
  11161.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  11162.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  11163.     where
  11164.          o.type in ('U','V','S')
  11165.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  11166.     and     (
  11167.         suser_id() = 1     /* User is the System Administrator */
  11168.         or o.uid = user_id()     /* User created the object */
  11169.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  11170.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  11171.             from sysprotects p
  11172.             where p.id = o.id
  11173.             /* get rows for public,current user,user's group */
  11174.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  11175.             /* check for SELECT,EXECUTE privilege */
  11176.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  11177.             ) = 1    /* final magic...compare Grants      */
  11178.            )
  11179.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11180.     and    (@column_name is null or @column_name = c.name)
  11181.     and     o.id = c.id
  11182.     and     t.type = d.ss_dtype
  11183.     and    (t.usertype != 80 or d.type_name='timestamp')
  11184.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11185.     and     c.usertype = t.usertype
  11186.     and    c.cdefault *= m.id
  11187.     and     m.colid = 1
  11188.     and    cfg.comment = 'default sortorder ID' 
  11189.     and    a_cha.type = 1001 /* type is charset */
  11190.     and    b_cha.type = 2001 /* type is sortorder */
  11191.     and    a_cha.id = b_cha.csid
  11192.     and     b_cha.id = cfg.value
  11193.     order by 2, 3, c.colid
  11194.     
  11195.     /* Fix ordinal positions */
  11196.     OPEN curColRowset
  11197.     FETCH NEXT FROM curColRowset
  11198.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11199.     
  11200.     /* Initialize tracking variables */
  11201.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  11202.  
  11203.     WHILE @@FETCH_STATUS = 0
  11204.     BEGIN
  11205.         /* Check to see if the catalog/schema/table */
  11206.         /* has changed since last row               */
  11207.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  11208.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  11209.         BEGIN
  11210.             /* Increment ordinal count */
  11211.             SELECT @currPos = @currPos + 1
  11212.         END
  11213.         ELSE
  11214.         BEGIN
  11215.             /* Set the catalog/schema/table to new values */
  11216.             SELECT @currCatalog = @TABLECATALOG,
  11217.                    @currSchema  = @TABLESCHEMA,
  11218.                    @currName    = @TABLENAME,
  11219.                    @currPos     = 1
  11220.         END
  11221.  
  11222.         /* Check for holes in the ordinal position order */
  11223.         if (@currPos < @ORDINALPOSITION)
  11224.         BEGIN        
  11225.             /* Update the ordinal position */
  11226.             UPDATE #columns_rowset
  11227.             SET    ORDINAL_POSITION = @currPos
  11228.             WHERE  TABLE_CATALOG = @TABLECATALOG
  11229.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  11230.               AND  TABLE_NAME    = @TABLENAME
  11231.               AND  COLUMN_NAME   = @COLUMNNAME
  11232.         END
  11233.  
  11234.         /* Fetch next result */
  11235.         FETCH NEXT FROM curColRowset
  11236.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11237.     END
  11238.  
  11239.     /* Cleanup cursor */
  11240.     CLOSE curColRowset
  11241.     DEALLOCATE curColRowset
  11242.  
  11243.     set nocount off
  11244.  
  11245.     /* Output the result set */
  11246.     select *
  11247.     from #columns_rowset
  11248.     order by 2, 3, 7
  11249. go
  11250.  
  11251. dump tran master with no_log
  11252. go
  11253.  
  11254. if (charindex('7.00', @@version) = 0)
  11255. begin
  11256.     print ''
  11257.     print ''
  11258.     print 'Warning:'
  11259.     print 'you are installing the stored procedures '
  11260.     print 'on a pre 7.0 SQL Server.'
  11261.     print 'Ignore the following errors.'
  11262. end
  11263. else
  11264.     drop proc sp_columns_rowset
  11265. go
  11266.  
  11267. /*    Procedure for 7.0 server */
  11268. create procedure sp_columns_rowset
  11269.     (
  11270.        @table_name    sysname,
  11271.     @table_schema     sysname = NULL,
  11272.     @column_name    sysname = NULL
  11273.     )
  11274. as
  11275.     set nocount on
  11276.  
  11277.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  11278.     CREATE TABLE #columns_rowset
  11279.     (
  11280.         TABLE_CATALOG            sysname,
  11281.         TABLE_SCHEMA            sysname,
  11282.         TABLE_NAME            sysname,
  11283.         COLUMN_NAME            sysname,
  11284.         COLUMN_GUID            uniqueidentifier    null,
  11285.         COLUMN_PROPID            int            null,
  11286.         ORDINAL_POSITION        int            IDENTITY(1,1),
  11287.         COLUMN_HASDEFAULT        bit,
  11288.         COLUMN_DEFAULT            nvarchar(2000),
  11289.         COLUMN_FLAGS            int,
  11290.         IS_NULLABLE            bit,
  11291.         DATA_TYPE            smallint,
  11292.         TYPE_GUID            uniqueidentifier    null,
  11293.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11294.         CHARACTER_OCTET_LENGTH        int            null,
  11295.         NUMERIC_PRECISION        smallint        null,
  11296.         NUMERIC_SCALE            smallint        null, 
  11297.         DATETIME_PRECISION        int            null,
  11298.         CHARACTER_SET_CATALOG        sysname            null,
  11299.         CHARACTER_SET_SCHEMA        sysname            null,
  11300.         CHARACTER_SET_NAME        sysname            null,
  11301.         COLLATION_CATALOG        sysname            null,
  11302.         COLLATION_SCHEMA        sysname            null,
  11303.         COLLATION_NAME            sysname            null,
  11304.         DOMAIN_CATALOG            sysname         null,
  11305.         DOMAIN_SCHEMA            sysname         null,
  11306.         DOMAIN_NAME            sysname         null,
  11307.         DESCRIPTION            nvarchar(1)
  11308.     )        
  11309.  
  11310.     /* Store results into the temp table */
  11311.     insert into #columns_rowset
  11312.         (
  11313.         TABLE_CATALOG,
  11314.         TABLE_SCHEMA,
  11315.         TABLE_NAME,
  11316.         COLUMN_NAME,
  11317.         COLUMN_GUID,
  11318.         COLUMN_PROPID,
  11319.         COLUMN_HASDEFAULT,
  11320.         COLUMN_DEFAULT,
  11321.         COLUMN_FLAGS,
  11322.         IS_NULLABLE,
  11323.         DATA_TYPE,
  11324.         TYPE_GUID,
  11325.         CHARACTER_MAXIMUM_LENGTH,
  11326.         CHARACTER_OCTET_LENGTH,
  11327.         NUMERIC_PRECISION,
  11328.         NUMERIC_SCALE,
  11329.         DATETIME_PRECISION,
  11330.         CHARACTER_SET_CATALOG,
  11331.         CHARACTER_SET_SCHEMA,
  11332.         CHARACTER_SET_NAME,
  11333.         COLLATION_CATALOG,
  11334.         COLLATION_SCHEMA,
  11335.         COLLATION_NAME,
  11336.         DOMAIN_CATALOG,
  11337.         DOMAIN_SCHEMA,
  11338.         DOMAIN_NAME,
  11339.         DESCRIPTION
  11340.         )
  11341.     select
  11342.         TABLE_CATALOG        = db_name(),
  11343.         TABLE_SCHEMA        = user_name(o.uid),
  11344.         TABLE_NAME        = o.name,
  11345.         COLUMN_NAME        = c.name,
  11346.         COLUMN_GUID        = convert(uniqueidentifier,null),
  11347.         COLUMN_PROPID        = convert(int,null),
  11348.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11349.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  11350.                         substring(m.text,2,datalength(m.text)/2-2)),
  11351.         COLUMN_FLAGS        = convert(int,
  11352.                         case when d.is_long = 1 
  11353.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11354.                         else 0
  11355.                         end
  11356.                     |    case when d.fixlen is not null
  11357.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11358.                         else 0
  11359.                         end
  11360.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  11361.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11362.                         else 0
  11363.                         end
  11364.                     |    case 
  11365.                         when d.type_name = 'timestamp'
  11366.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11367.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  11368.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  11369.                         else 0 
  11370.                         end),
  11371.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  11372.         DATA_TYPE        = d.oledb_data_type,
  11373.         TYPE_GUID        = convert(uniqueidentifier,null),
  11374.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11375.                         case 
  11376.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11377.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11378.                         then coalesce(d.column_size,c.length)
  11379.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11380.                         then coalesce(d.column_size,c.length/2)
  11381.                         else null 
  11382.                         end),
  11383.         CHARACTER_OCTET_LENGTH    = convert(int,
  11384.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11385.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11386.                         then coalesce(d.column_size,c.length)
  11387.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11388.                         then coalesce(d.column_size*2,c.length)
  11389.                         else null 
  11390.                         end),
  11391.         NUMERIC_PRECISION    = convert(smallint,
  11392.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  11393.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11394.         NUMERIC_SCALE        = convert(smallint, 
  11395.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11396.         DATETIME_PRECISION    = convert(int,
  11397.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11398.                             when data_precision = 23 then 3 else 0 end),
  11399.         CHARACTER_SET_CATALOG    = convert(sysname,
  11400.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11401.                         then N'master' 
  11402.                         else null 
  11403.                         end),
  11404.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11405.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11406.                         then N'dbo' 
  11407.                         else null 
  11408.                         end),
  11409.         CHARACTER_SET_NAME    = convert(sysname,
  11410.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11411.                         then a_cha.name 
  11412.                         else null 
  11413.                         end),
  11414.         COLLATION_CATALOG    = convert(sysname,
  11415.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11416.                         then N'master' 
  11417.                         else null 
  11418.                         end),
  11419.         COLLATION_SCHEMA    = convert(sysname,
  11420.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11421.                         then N'dbo' 
  11422.                         else null 
  11423.                         end),
  11424.         COLLATION_NAME        = convert(sysname,
  11425.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11426.                         then b_cha.name 
  11427.                         else null 
  11428.                         end),
  11429.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11430.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11431.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11432.         DESCRIPTION        = convert(nvarchar(1),null)    
  11433.  
  11434.     from
  11435.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  11436.         sysobjects o,
  11437.         master.dbo.spt_provider_types d,
  11438.         systypes t,
  11439.         master.dbo.sysconfigures    cfg,
  11440.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  11441.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  11442.     where
  11443.         permissions(o.id, c.name) <> 0
  11444.     and    o.name = @table_name
  11445.     and     o.type in ('U','V','S')
  11446.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11447.     and    (@column_name is null or @column_name = c.name)
  11448.     and     o.id = c.id
  11449.     and     t.xtype = d.ss_dtype
  11450.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11451.     and     c.xusertype = t.xusertype
  11452.     and    cfg.comment = 'default sortorder id' 
  11453.     and    a_cha.type = 1001 /* type is charset */
  11454.     and    b_cha.type = 2001 /* type is sortorder */
  11455.     and    a_cha.id = b_cha.csid
  11456.     and     b_cha.id = cfg.value
  11457.     order by 2, 3, c.colorder
  11458.  
  11459.     set nocount off
  11460.  
  11461.     /* Output the result set */
  11462.     select *
  11463.     from #columns_rowset
  11464.     order by 2, 3, 7
  11465. go
  11466.  
  11467. dump tran master with no_log
  11468. go
  11469.  
  11470. /*    Procedure for 7.0 server */
  11471. create procedure sp_columns_rowset;2
  11472.     (
  11473.     @table_schema     sysname = NULL,
  11474.     @column_name    sysname = NULL
  11475.     )
  11476. as     
  11477.     set nocount on
  11478.  
  11479.     /* Declare bindings */
  11480.     DECLARE @TABLECATALOG    sysname,
  11481.         @TABLESCHEMA     sysname,
  11482.         @TABLENAME       sysname,
  11483.         @COLUMNNAME      sysname,
  11484.         @ORDINALPOSITION int
  11485.  
  11486.     /* Declare tracking variables */
  11487.     DECLARE @currCatalog sysname,
  11488.          @currSchema  sysname,
  11489.         @currName    sysname,
  11490.         @currPos     int
  11491.  
  11492.     /* Create temp table since ORDINAL_POSITION isn't maintained in syscolumns */
  11493.     CREATE TABLE #columns_rowset
  11494.     (
  11495.         TABLE_CATALOG            sysname,
  11496.         TABLE_SCHEMA            sysname,
  11497.         TABLE_NAME            sysname,
  11498.         COLUMN_NAME            sysname,
  11499.         COLUMN_GUID            uniqueidentifier    null,
  11500.         COLUMN_PROPID            int            null,
  11501.         ORDINAL_POSITION        int            null,
  11502.         COLUMN_HASDEFAULT        bit,
  11503.         COLUMN_DEFAULT            nvarchar(2000),
  11504.         COLUMN_FLAGS            int,
  11505.         IS_NULLABLE            bit,
  11506.         DATA_TYPE            smallint,
  11507.         TYPE_GUID            uniqueidentifier    null,
  11508.         CHARACTER_MAXIMUM_LENGTH    int            null,
  11509.         CHARACTER_OCTET_LENGTH        int            null,
  11510.         NUMERIC_PRECISION        smallint        null,
  11511.         NUMERIC_SCALE            smallint        null, 
  11512.         DATETIME_PRECISION        int            null,
  11513.         CHARACTER_SET_CATALOG        sysname            null,
  11514.         CHARACTER_SET_SCHEMA        sysname            null,
  11515.         CHARACTER_SET_NAME        sysname            null,
  11516.         COLLATION_CATALOG        sysname            null,
  11517.         COLLATION_SCHEMA        sysname            null,
  11518.         COLLATION_NAME            sysname            null,
  11519.         DOMAIN_CATALOG            sysname         null,
  11520.         DOMAIN_SCHEMA            sysname         null,
  11521.         DOMAIN_NAME            sysname         null,
  11522.         DESCRIPTION            nvarchar(1)
  11523.     )        
  11524.  
  11525.     /* Declare cursor for fixup */
  11526.     DECLARE curColRowset CURSOR
  11527.     FOR SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION
  11528.         FROM #columns_rowset
  11529.         ORDER BY TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION
  11530.  
  11531.     /* Store results into the temp table */
  11532.     insert into #columns_rowset
  11533.         (
  11534.         TABLE_CATALOG,
  11535.         TABLE_SCHEMA,
  11536.         TABLE_NAME,
  11537.         COLUMN_NAME,
  11538.         COLUMN_GUID,
  11539.         COLUMN_PROPID,
  11540.         ORDINAL_POSITION,
  11541.         COLUMN_HASDEFAULT,
  11542.         COLUMN_DEFAULT,
  11543.         COLUMN_FLAGS,
  11544.         IS_NULLABLE,
  11545.         DATA_TYPE,
  11546.         TYPE_GUID,
  11547.         CHARACTER_MAXIMUM_LENGTH,
  11548.         CHARACTER_OCTET_LENGTH,
  11549.         NUMERIC_PRECISION,
  11550.         NUMERIC_SCALE,
  11551.         DATETIME_PRECISION,
  11552.         CHARACTER_SET_CATALOG,
  11553.         CHARACTER_SET_SCHEMA,
  11554.         CHARACTER_SET_NAME,
  11555.         COLLATION_CATALOG,
  11556.         COLLATION_SCHEMA,
  11557.         COLLATION_NAME,
  11558.         DOMAIN_CATALOG,
  11559.         DOMAIN_SCHEMA,
  11560.         DOMAIN_NAME,
  11561.         DESCRIPTION
  11562.         )
  11563.  
  11564.     select
  11565.         TABLE_CATALOG        = db_name(),
  11566.         TABLE_SCHEMA        = user_name(o.uid),
  11567.         TABLE_NAME        = o.name,
  11568.         COLUMN_NAME        = c.name,
  11569.         COLUMN_GUID        = convert(uniqueidentifier,null),
  11570.         COLUMN_PROPID        = convert(int,null),
  11571.         ORDINAL_POSITION    = convert(int, c.colorder),
  11572.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  11573.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  11574.                         substring(m.text,2,datalength(m.text)/2-2)),
  11575.         COLUMN_FLAGS        = convert(int,
  11576.                         case when d.is_long = 1 
  11577.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  11578.                         else 0
  11579.                         end
  11580.                     |    case when d.fixlen is not null
  11581.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  11582.                         else 0
  11583.                         end
  11584.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  11585.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  11586.                         else 0
  11587.                         end
  11588.                     |    case 
  11589.                         when d.type_name = 'timestamp'
  11590.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  11591.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  11592.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  11593.                         else 0 
  11594.                         end),
  11595.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  11596.         DATA_TYPE        = d.oledb_data_type,
  11597.         TYPE_GUID        = convert(uniqueidentifier,null),
  11598.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  11599.                         case 
  11600.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11601.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11602.                         then coalesce(d.column_size,c.length)
  11603.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11604.                         then coalesce(d.column_size,c.length/2)
  11605.                         else null 
  11606.                         end),
  11607.         CHARACTER_OCTET_LENGTH    = convert(int,
  11608.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  11609.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  11610.                         then coalesce(d.column_size,c.length)
  11611.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  11612.                         then coalesce(d.column_size*2,c.length)
  11613.                         else null 
  11614.                         end),
  11615.         NUMERIC_PRECISION    = convert(smallint,
  11616.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  11617.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  11618.         NUMERIC_SCALE        = convert(smallint, 
  11619.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  11620.         DATETIME_PRECISION    = convert(int,
  11621.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  11622.                             when data_precision = 23 then 3 else 0 end),
  11623.         CHARACTER_SET_CATALOG    = convert(sysname,
  11624.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11625.                         then N'master' 
  11626.                         else null 
  11627.                         end),
  11628.         CHARACTER_SET_SCHEMA    = convert(sysname,
  11629.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11630.                         then N'dbo' 
  11631.                         else null 
  11632.                         end),
  11633.         CHARACTER_SET_NAME    = convert(sysname,
  11634.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11635.                         then a_cha.name 
  11636.                         else null 
  11637.                         end),
  11638.         COLLATION_CATALOG    = convert(sysname,
  11639.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11640.                         then N'master' 
  11641.                         else null 
  11642.                         end),
  11643.         COLLATION_SCHEMA    = convert(sysname,
  11644.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11645.                         then N'dbo' 
  11646.                         else null 
  11647.                         end),
  11648.         COLLATION_NAME        = convert(sysname,
  11649.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  11650.                         then b_cha.name 
  11651.                         else null 
  11652.                         end),
  11653.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  11654.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  11655.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  11656.         DESCRIPTION        = convert(nvarchar(1),null)    
  11657.     
  11658.     from
  11659.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  11660.         sysobjects o,
  11661.         master.dbo.spt_provider_types d,
  11662.         systypes t,
  11663.         master.dbo.sysconfigures    cfg,
  11664.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  11665.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  11666.     where
  11667.         permissions(o.id, c.name) <> 0
  11668.     and     o.type in ('U','V','S')
  11669.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  11670.     and    (@column_name is null or @column_name = c.name)
  11671.     and     o.id = c.id
  11672.     and     t.xtype = d.ss_dtype
  11673.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  11674.     and     c.xusertype = t.xusertype
  11675.     and    cfg.comment = 'default sortorder id' 
  11676.     and     a_cha.type = 1001 /* type is charset */
  11677.     and     b_cha.type = 2001 /* type is sortorder */
  11678.     and    a_cha.id = b_cha.csid
  11679.     and     b_cha.id = cfg.value
  11680.     order by 2, 3, c.colorder
  11681.  
  11682.     /* Fix ordinal positions */
  11683.     OPEN curColRowset
  11684.     FETCH NEXT FROM curColRowset
  11685.     INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11686.     
  11687.     /* Initialize tracking variables */
  11688.     SELECT @currCatalog = NULL, @currSchema = NULL, @currName = NULL
  11689.  
  11690.     WHILE @@FETCH_STATUS = 0
  11691.     BEGIN
  11692.         /* Check to see if the catalog/schema/table */
  11693.         /* has changed since last row               */
  11694.         IF (@currCatalog   + '.' + @currSchema   + '.' + @currName =
  11695.             @TABLECATALOG  + '.' + @TABLESCHEMA  + '.' + @TABLENAME)
  11696.         BEGIN
  11697.             /* Increment ordinal count */
  11698.             SELECT @currPos = @currPos + 1
  11699.         END
  11700.         ELSE
  11701.         BEGIN
  11702.             /* Set the catalog/schema/table to new values */
  11703.             SELECT @currCatalog = @TABLECATALOG,
  11704.                    @currSchema  = @TABLESCHEMA,
  11705.                    @currName    = @TABLENAME,
  11706.                    @currPos     = 1
  11707.         END
  11708.  
  11709.         /* Check for holes in the ordinal position order */
  11710.         if (@currPos < @ORDINALPOSITION)
  11711.         BEGIN        
  11712.             /* Update the ordinal position */
  11713.             UPDATE #columns_rowset
  11714.             SET    ORDINAL_POSITION = @currPos
  11715.             WHERE  TABLE_CATALOG = @TABLECATALOG
  11716.               AND  TABLE_SCHEMA  = @TABLESCHEMA
  11717.               AND  TABLE_NAME    = @TABLENAME
  11718.               AND  COLUMN_NAME   = @COLUMNNAME
  11719.         END
  11720.  
  11721.         /* Fetch next result */
  11722.         FETCH NEXT FROM curColRowset
  11723.         INTO @TABLECATALOG, @TABLESCHEMA, @TABLENAME, @COLUMNNAME, @ORDINALPOSITION
  11724.     END
  11725.  
  11726.     /* Cleanup cursor */
  11727.     CLOSE curColRowset
  11728.     DEALLOCATE curColRowset
  11729.  
  11730.     set nocount off
  11731.  
  11732.     /* Output the result set */
  11733.     select     *
  11734.     from #columns_rowset
  11735.     order by 2, 3, 7
  11736. go
  11737.  
  11738. dump tran master with no_log
  11739. go
  11740.  
  11741. /*    Procedure for 7.0 server */
  11742. create procedure sp_columns_rowset;5
  11743.     (
  11744.     @table_server        sysname,
  11745.     @table_catalog        sysname = null,
  11746.     @table_name        sysname = null,
  11747.     @table_schema        sysname = null,
  11748.     @column_name        sysname = null
  11749.     )
  11750. as
  11751.     select
  11752.         TABLE_CATALOG,
  11753.         TABLE_SCHEMA,
  11754.         TABLE_NAME,
  11755.         COLUMN_NAME,
  11756.         COLUMN_GUID,
  11757.         COLUMN_PROPID,
  11758.         ORDINAL_POSITION,
  11759.         COLUMN_HASDEFAULT,
  11760.         COLUMN_DEFAULT,
  11761.         COLUMN_FLAGS,
  11762.         IS_NULLABLE,
  11763.         DATA_TYPE,
  11764.         TYPE_GUID,
  11765.         CHARACTER_MAXIMUM_LENGTH,
  11766.         CHARACTER_OCTET_LENGTH,
  11767.         NUMERIC_PRECISION,
  11768.         NUMERIC_SCALE,
  11769.         DATETIME_PRECISION,
  11770.         CHARACTER_SET_CATALOG,
  11771.         CHARACTER_SET_SCHEMA,
  11772.         CHARACTER_SET_NAME,
  11773.         COLLATION_CATALOG,
  11774.         COLLATION_SCHEMA,
  11775.         COLLATION_NAME,
  11776.         DOMAIN_CATALOG,
  11777.         DOMAIN_SCHEMA,
  11778.         DOMAIN_NAME,
  11779.         DESCRIPTION
  11780.     from master.dbo.SYSREMOTE_COLUMNS <
  11781.                 @table_server,
  11782.                 @table_catalog,
  11783.                 @table_schema,
  11784.                 @table_name,
  11785.                 @column_name > 
  11786.     order by 1, 2, 3, 7
  11787. go
  11788.  
  11789.  
  11790. dump tran master with no_log
  11791. go
  11792.  
  11793. grant execute on sp_columns_rowset to public
  11794. go
  11795.  
  11796. dump tran master with no_log
  11797. go
  11798. if (charindex('6.00', @@version) > 0)
  11799.     begin
  11800.     if (exists (select * from sysobjects
  11801.             where name = 'sp_columns_rowset' and type = 'P '))
  11802.         begin
  11803.         drop procedure sp_columns_rowset
  11804.         dump tran master with no_log
  11805.         end
  11806.     end
  11807. go
  11808.  
  11809. print ''
  11810. print 'creating sp_foreign_keys_rowset'
  11811. go
  11812.  
  11813. /*    Procedure for 6.0 and 6.5 server */
  11814. CREATE PROCEDURE sp_foreign_keys_rowset
  11815.     (
  11816.      @pk_table_name        varchar(255) = null,
  11817.     @pk_table_schema    varchar(255) = null,
  11818.     @pk_table_catalog    varchar(255) = null,
  11819.     @fk_table_name        varchar(255) = null,
  11820.     @fk_table_schema    varchar(255) = null,
  11821.     @fk_table_catalog    varchar(255) = null
  11822.     )
  11823. as
  11824. IF @pk_table_name is not null
  11825.     BEGIN
  11826.     select
  11827.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11828.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11829.         PK_TABLE_NAME         = o1.name,
  11830.         PK_COLUMN_NAME         = c1.name,
  11831.         PK_COLUMN_GUID        = convert(binary(16),null),
  11832.         PK_COLUMN_PROPID    = convert(int,null),
  11833.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11834.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11835.         FK_TABLE_NAME         = o2.name,
  11836.         FK_COLUMN_NAME         = c2.name,
  11837.         FK_COLUMN_GUID        = convert(binary(16),null),
  11838.         FK_COLUMN_PROPID    = convert(int,null),
  11839.         ORDINAL                = convert(int,1),
  11840.         UPDATE_RULE            = 'NO ACTION',
  11841.         DELETE_RULE         = 'NO ACTION',
  11842.         PK_NAME                = object_name(s.constid),
  11843.         FK_NAME                = object_name(r.constid),
  11844.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11845.     from
  11846.         sysobjects o1, sysobjects o2,
  11847.         syscolumns c1, syscolumns c2,
  11848.         sysreferences r, sysconstraints s
  11849.     where    
  11850.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11851.     and    o1.name = @pk_table_name
  11852.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11853.     and    o1.id = r.rkeyid
  11854.     and s.id = o1.id and (s.status & 0xf) = 1
  11855.     and    o1.id = c1.id
  11856.     and    c1.colid = r.rkey1
  11857.     and    r.fkeyid = o2.id
  11858.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11859.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11860.     and    o2.id = c2.id    
  11861.     and    c2.colid = r.fkey1
  11862.     union all
  11863.     select
  11864.         db_name(r.rkeydbid),
  11865.         user_name(o1.uid),
  11866.         o1.name,
  11867.         c1.name,
  11868.         convert(binary(16),null),
  11869.         convert(int,null),
  11870.         db_name(r.fkeydbid),
  11871.         user_name(o2.uid),
  11872.         o2.name,
  11873.         c2.name,
  11874.         convert(binary(16),null),
  11875.         convert(int,null),
  11876.         convert(int,2),
  11877.         'NO ACTION',
  11878.         'NO ACTION',
  11879.         object_name(s.constid),
  11880.         object_name(r.constid),
  11881.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11882.     from
  11883.         sysobjects o1, sysobjects o2,
  11884.         syscolumns c1, syscolumns c2,
  11885.         sysreferences r, sysconstraints s
  11886.     where    
  11887.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11888.     and    o1.name = @pk_table_name
  11889.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11890.     and    o1.id = r.rkeyid
  11891.     and s.id = o1.id and (s.status & 0xf) = 1
  11892.      and    o1.id = c1.id
  11893.     and    c1.colid = r.rkey2
  11894.     and    r.keycnt >= 2
  11895.     and r.fkeyid = o2.id
  11896.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11897.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11898.     and    o2.id = c2.id    
  11899.     and    c2.colid = r.fkey2
  11900.     union all
  11901.     select
  11902.         db_name(r.rkeydbid),
  11903.         user_name(o1.uid),
  11904.         o1.name,
  11905.         c1.name,
  11906.         convert(binary(16),null),
  11907.         convert(int,null),
  11908.         db_name(r.fkeydbid),
  11909.         user_name(o2.uid),
  11910.         o2.name,
  11911.         c2.name,
  11912.         convert(binary(16),null),
  11913.         convert(int,null),
  11914.         convert(int,3),
  11915.         'NO ACTION',
  11916.         'NO ACTION',
  11917.         object_name(s.constid),
  11918.         object_name(r.constid),
  11919.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11920.     from
  11921.         sysobjects o1, sysobjects o2,
  11922.         syscolumns c1, syscolumns c2,
  11923.         sysreferences r, sysconstraints s
  11924.     where    
  11925.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11926.     and    o1.name = @pk_table_name
  11927.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11928.     and    o1.id = r.rkeyid
  11929.     and s.id = o1.id and (s.status & 0xf) = 1
  11930.     and    o1.id = c1.id
  11931.     and    c1.colid = r.rkey3
  11932.     and    r.keycnt >= 3
  11933.     and r.fkeyid = o2.id
  11934.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11935.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11936.     and    o2.id = c2.id    
  11937.     and    c2.colid = r.fkey3
  11938.     union all
  11939.     select
  11940.         db_name(r.rkeydbid),
  11941.         user_name(o1.uid),
  11942.         o1.name,
  11943.         c1.name,
  11944.         convert(binary(16),null),
  11945.         convert(int,null),
  11946.         db_name(r.fkeydbid),
  11947.         user_name(o2.uid),
  11948.         o2.name,
  11949.         c2.name,
  11950.         convert(binary(16),null),
  11951.         convert(int,null),
  11952.         convert(int,4),
  11953.         'NO ACTION',
  11954.         'NO ACTION',
  11955.         object_name(s.constid),
  11956.         object_name(r.constid),
  11957.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11958.     from
  11959.         sysobjects o1, sysobjects o2,
  11960.         syscolumns c1, syscolumns c2,
  11961.         sysreferences r, sysconstraints s
  11962.     where    
  11963.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11964.     and    o1.name = @pk_table_name
  11965.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11966.     and    o1.id = r.rkeyid
  11967.     and s.id = o1.id and (s.status & 0xf) = 1
  11968.     and    r.keycnt >= 4
  11969.     and    o1.id = c1.id
  11970.     and    c1.colid = r.rkey4
  11971.     and    r.fkeyid = o2.id
  11972.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11973.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11974.     and    o2.id = c2.id    
  11975.     and    c2.colid = r.fkey4
  11976.     union all
  11977.     select
  11978.         db_name(r.rkeydbid),
  11979.         user_name(o1.uid),
  11980.         o1.name,
  11981.         c1.name,
  11982.         convert(binary(16),null),
  11983.         convert(int,null),
  11984.         db_name(r.fkeydbid),
  11985.         user_name(o2.uid),
  11986.         o2.name,
  11987.         c2.name,
  11988.         convert(binary(16),null),
  11989.         convert(int,null),
  11990.         convert(int,5),
  11991.         'NO ACTION',
  11992.         'NO ACTION',
  11993.         object_name(s.constid),
  11994.         object_name(r.constid),
  11995.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11996.     from
  11997.         sysobjects o1, sysobjects o2,
  11998.         syscolumns c1, syscolumns c2,
  11999.         sysreferences r, sysconstraints s
  12000.     where    
  12001.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12002.     and    o1.name = @pk_table_name
  12003.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12004.     and    o1.id = r.rkeyid
  12005.     and s.id = o1.id and (s.status & 0xf) = 1
  12006.     and    r.keycnt >= 5
  12007.     and    o1.id = c1.id
  12008.     and    c1.colid = r.rkey5
  12009.     and r.fkeyid = o2.id
  12010.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12011.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12012.     and    o2.id = c2.id    
  12013.     and    c2.colid = r.fkey5
  12014.     union all
  12015.     select
  12016.         db_name(r.rkeydbid),
  12017.         user_name(o1.uid),
  12018.         o1.name,
  12019.         c1.name,
  12020.         convert(binary(16),null),
  12021.         convert(int,null),
  12022.         db_name(r.fkeydbid),
  12023.         user_name(o2.uid),
  12024.         o2.name,
  12025.         c2.name,
  12026.         convert(binary(16),null),
  12027.         convert(int,null),
  12028.         convert(int,6),
  12029.         'NO ACTION',
  12030.         'NO ACTION',
  12031.         object_name(s.constid),
  12032.         object_name(r.constid),
  12033.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12034.     from
  12035.         sysobjects o1, sysobjects o2,
  12036.         syscolumns c1, syscolumns c2,
  12037.         sysreferences r, sysconstraints s
  12038.     where    
  12039.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12040.     and    o1.name = @pk_table_name
  12041.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12042.     and    o1.id = r.rkeyid
  12043.     and s.id = o1.id and (s.status & 0xf) = 1
  12044.     and    r.keycnt >= 6
  12045.     and    o1.id = c1.id
  12046.     and    c1.colid = r.rkey6
  12047.     and r.fkeyid = o2.id
  12048.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12049.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12050.     and    o2.id = c2.id    
  12051.     and    c2.colid = r.fkey6
  12052.     union all
  12053.     select
  12054.         db_name(r.rkeydbid),
  12055.         user_name(o1.uid),
  12056.         o1.name,
  12057.         c1.name,
  12058.         convert(binary(16),null),
  12059.         convert(int,null),
  12060.         db_name(r.fkeydbid),
  12061.         user_name(o2.uid),
  12062.         o2.name,
  12063.         c2.name,
  12064.         convert(binary(16),null),
  12065.         convert(int,null),
  12066.         convert(int,7),
  12067.         'NO ACTION',
  12068.         'NO ACTION',
  12069.         object_name(s.constid),
  12070.         object_name(r.constid),
  12071.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12072.     from
  12073.         sysobjects o1, sysobjects o2,
  12074.         syscolumns c1, syscolumns c2,
  12075.         sysreferences r, sysconstraints s
  12076.     where    
  12077.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12078.     and    o1.name = @pk_table_name
  12079.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12080.     and    o1.id = r.rkeyid
  12081.     and s.id = o1.id and (s.status & 0xf) = 1
  12082.     and    r.keycnt >= 7
  12083.     and    o1.id = c1.id
  12084.     and    c1.colid = r.rkey7
  12085.     and r.fkeyid = o2.id
  12086.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12087.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12088.     and    o2.id = c2.id    
  12089.     and    c2.colid = r.fkey7
  12090.     union all
  12091.     select
  12092.         db_name(r.rkeydbid),
  12093.         user_name(o1.uid),
  12094.         o1.name,
  12095.         c1.name,
  12096.         convert(binary(16),null),
  12097.         convert(int,null),
  12098.         db_name(r.fkeydbid),
  12099.         user_name(o2.uid),
  12100.         o2.name,
  12101.         c2.name,
  12102.         convert(binary(16),null),
  12103.         convert(int,null),
  12104.         convert(int,8),
  12105.         'NO ACTION',
  12106.         'NO ACTION',
  12107.         object_name(s.constid),
  12108.         object_name(r.constid),
  12109.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12110.     from
  12111.         sysobjects o1, sysobjects o2,
  12112.         syscolumns c1, syscolumns c2,
  12113.         sysreferences r, sysconstraints s
  12114.     where    
  12115.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12116.     and    o1.name = @pk_table_name
  12117.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12118.     and    o1.id = r.rkeyid
  12119.     and s.id = o1.id and (s.status & 0xf) = 1
  12120.     and    r.keycnt >= 8
  12121.     and    o1.id = c1.id
  12122.     and    c1.colid = r.rkey8
  12123.     and r.fkeyid = o2.id
  12124.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12125.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12126.     and    o2.id = c2.id    
  12127.     and    c2.colid = r.fkey8
  12128.     union all
  12129.     select
  12130.         db_name(r.rkeydbid),
  12131.         user_name(o1.uid),
  12132.         o1.name,
  12133.         c1.name,
  12134.         convert(binary(16),null),
  12135.         convert(int,null),
  12136.         db_name(r.fkeydbid),
  12137.         user_name(o2.uid),
  12138.         o2.name,
  12139.         c2.name,
  12140.         convert(binary(16),null),
  12141.         convert(int,null),
  12142.         convert(int,9),
  12143.         'NO ACTION',
  12144.         'NO ACTION',
  12145.         object_name(s.constid),
  12146.         object_name(r.constid),
  12147.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12148.     from
  12149.         sysobjects o1, sysobjects o2,
  12150.         syscolumns c1, syscolumns c2,
  12151.         sysreferences r, sysconstraints s
  12152.     where    
  12153.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12154.     and    o1.name = @pk_table_name
  12155.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12156.     and    o1.id = r.rkeyid
  12157.     and s.id = o1.id and (s.status & 0xf) = 1
  12158.     and    r.keycnt >= 9
  12159.     and    o1.id = c1.id
  12160.     and    c1.colid = r.rkey9
  12161.     and r.fkeyid = o2.id
  12162.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12163.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12164.     and    o2.id = c2.id    
  12165.     and    c2.colid = r.fkey9
  12166.     union all
  12167.     select
  12168.         db_name(r.rkeydbid),
  12169.         user_name(o1.uid),
  12170.         o1.name,
  12171.         c1.name,
  12172.         convert(binary(16),null),
  12173.         convert(int,null),
  12174.         db_name(r.fkeydbid),
  12175.         user_name(o2.uid),
  12176.         o2.name,
  12177.         c2.name,
  12178.         convert(binary(16),null),
  12179.         convert(int,null),
  12180.         convert(int,10),
  12181.         'NO ACTION',
  12182.         'NO ACTION',
  12183.         object_name(s.constid),
  12184.         object_name(r.constid),
  12185.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/    
  12186.     from
  12187.         sysobjects o1, sysobjects o2,
  12188.         syscolumns c1, syscolumns c2,
  12189.         sysreferences r, sysconstraints s
  12190.     where    
  12191.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12192.     and    o1.name = @pk_table_name
  12193.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12194.     and    o1.id = r.rkeyid
  12195.     and s.id = o1.id and (s.status & 0xf) = 1
  12196.     and    r.keycnt >= 10
  12197.     and    o1.id = c1.id
  12198.     and    c1.colid = r.rkey10
  12199.     and r.fkeyid = o2.id
  12200.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12201.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12202.     and    o2.id = c2.id    
  12203.     and    c2.colid = r.fkey10
  12204.     union all
  12205.     select
  12206.         db_name(r.rkeydbid),
  12207.         user_name(o1.uid),
  12208.         o1.name,
  12209.         c1.name,
  12210.         convert(binary(16),null),
  12211.         convert(int,null),
  12212.         db_name(r.fkeydbid),
  12213.         user_name(o2.uid),
  12214.         o2.name,
  12215.         c2.name,
  12216.         convert(binary(16),null),
  12217.         convert(int,null),
  12218.         convert(int,11),
  12219.         'NO ACTION',
  12220.         'NO ACTION',
  12221.         object_name(s.constid),
  12222.         object_name(r.constid),
  12223.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12224.     from
  12225.         sysobjects o1, sysobjects o2,
  12226.         syscolumns c1, syscolumns c2,
  12227.         sysreferences r, sysconstraints s
  12228.     where    
  12229.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12230.     and    o1.name = @pk_table_name
  12231.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12232.     and    o1.id = r.rkeyid
  12233.     and s.id = o1.id and (s.status & 0xf) = 1
  12234.     and    r.keycnt >= 11
  12235.     and    o1.id = c1.id
  12236.     and    c1.colid = r.rkey11
  12237.     and r.fkeyid = o2.id
  12238.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12239.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12240.     and    o2.id = c2.id    
  12241.     and    c2.colid = r.fkey11
  12242.     union all
  12243.     select
  12244.         db_name(r.rkeydbid),
  12245.         user_name(o1.uid),
  12246.         o1.name,
  12247.         c1.name,
  12248.         convert(binary(16),null),
  12249.         convert(int,null),
  12250.         db_name(r.fkeydbid),
  12251.         user_name(o2.uid),
  12252.         o2.name,
  12253.         c2.name,
  12254.         convert(binary(16),null),
  12255.         convert(int,null),
  12256.         convert(int,12),
  12257.         'NO ACTION',
  12258.         'NO ACTION',
  12259.         object_name(s.constid),
  12260.         object_name(r.constid),
  12261.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12262.     from
  12263.         sysobjects o1, sysobjects o2,
  12264.         syscolumns c1, syscolumns c2,
  12265.         sysreferences r, sysconstraints s
  12266.     where    
  12267.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12268.     and    o1.name = @pk_table_name
  12269.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12270.     and    o1.id = r.rkeyid
  12271.     and s.id = o1.id and (s.status & 0xf) = 1
  12272.     and    r.keycnt >= 12
  12273.     and    o1.id = c1.id
  12274.     and    c1.colid = r.rkey12
  12275.     and r.fkeyid = o2.id
  12276.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12277.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12278.     and    o2.id = c2.id    
  12279.     and    c2.colid = r.fkey12
  12280.     union all
  12281.     select
  12282.         db_name(r.rkeydbid),
  12283.         user_name(o1.uid),
  12284.         o1.name,
  12285.         c1.name,
  12286.         convert(binary(16),null),
  12287.         convert(int,null),
  12288.         db_name(r.fkeydbid),
  12289.         user_name(o2.uid),
  12290.         o2.name,
  12291.         c2.name,
  12292.         convert(binary(16),null),
  12293.         convert(int,null),
  12294.         convert(int,13),
  12295.         'NO ACTION',
  12296.         'NO ACTION',
  12297.         object_name(s.constid),
  12298.         object_name(r.constid),
  12299.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12300.     from
  12301.         sysobjects o1, sysobjects o2,
  12302.         syscolumns c1, syscolumns c2,
  12303.         sysreferences r, sysconstraints s
  12304.     where    
  12305.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12306.     and    o1.name = @pk_table_name
  12307.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12308.     and    o1.id = r.rkeyid
  12309.     and s.id = o1.id and (s.status & 0xf) = 1
  12310.     and    r.keycnt >= 13
  12311.     and    o1.id = c1.id
  12312.     and    c1.colid = r.rkey13
  12313.     and r.fkeyid = o2.id
  12314.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12315.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12316.     and    o2.id = c2.id    
  12317.     and    c2.colid = r.fkey13
  12318.     union all
  12319.     select
  12320.         db_name(r.rkeydbid),
  12321.         user_name(o1.uid),
  12322.         o1.name,
  12323.         c1.name,
  12324.         convert(binary(16),null),
  12325.         convert(int,null),
  12326.         db_name(r.fkeydbid),
  12327.         user_name(o2.uid),
  12328.         o2.name,
  12329.         c2.name,
  12330.         convert(binary(16),null),
  12331.         convert(int,null),
  12332.         convert(int,14),
  12333.         'NO ACTION',
  12334.         'NO ACTION',
  12335.         object_name(s.constid),
  12336.         object_name(r.constid),
  12337.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12338.     from
  12339.         sysobjects o1, sysobjects o2,
  12340.         syscolumns c1, syscolumns c2,
  12341.         sysreferences r, sysconstraints s
  12342.     where    
  12343.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12344.     and    o1.name = @pk_table_name
  12345.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12346.     and    o1.id = r.rkeyid
  12347.     and s.id = o1.id and (s.status & 0xf) = 1
  12348.     and    r.keycnt >= 14
  12349.     and    o1.id = c1.id
  12350.     and    c1.colid = r.rkey14
  12351.     and r.fkeyid = o2.id
  12352.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12353.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12354.     and    o2.id = c2.id    
  12355.     and    c2.colid = r.fkey14
  12356.     union all
  12357.     select
  12358.         db_name(r.rkeydbid),
  12359.         user_name(o1.uid),
  12360.         o1.name,
  12361.         c1.name,
  12362.         convert(binary(16),null),
  12363.         convert(int,null),
  12364.         db_name(r.fkeydbid),
  12365.         user_name(o2.uid),
  12366.         o2.name,
  12367.         c2.name,
  12368.         convert(binary(16),null),
  12369.         convert(int,null),
  12370.         convert(int,15),
  12371.         'NO ACTION',
  12372.         'NO ACTION',
  12373.         object_name(s.constid),
  12374.         object_name(r.constid),
  12375.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12376.     from
  12377.         sysobjects o1, sysobjects o2,
  12378.         syscolumns c1, syscolumns c2,
  12379.         sysreferences r, sysconstraints s
  12380.     where    
  12381.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12382.     and    o1.name = @pk_table_name
  12383.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12384.     and    o1.id = r.rkeyid
  12385.     and s.id = o1.id and (s.status & 0xf) = 1
  12386.     and    r.keycnt >= 15
  12387.     and    o1.id = c1.id
  12388.     and    c1.colid = r.rkey15
  12389.     and r.fkeyid = o2.id
  12390.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12391.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12392.     and    o2.id = c2.id    
  12393.     and    c2.colid = r.fkey15
  12394.     union all
  12395.     select
  12396.         db_name(r.rkeydbid),
  12397.         user_name(o1.uid),
  12398.         o1.name,
  12399.         c1.name,
  12400.         convert(binary(16),null),
  12401.         convert(int,null),
  12402.         db_name(r.fkeydbid),
  12403.         user_name(o2.uid),
  12404.         o2.name,
  12405.         c2.name,
  12406.         convert(binary(16),null),
  12407.         convert(int,null),
  12408.         convert(int,16),
  12409.         'NO ACTION',
  12410.         'NO ACTION',
  12411.         object_name(s.constid),
  12412.         object_name(r.constid),
  12413.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12414.     from
  12415.         sysobjects o1, sysobjects o2,
  12416.         syscolumns c1, syscolumns c2,
  12417.         sysreferences r, sysconstraints s
  12418.     where    
  12419.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12420.     and    o1.name = @pk_table_name
  12421.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12422.     and    o1.id = r.rkeyid
  12423.     and s.id = o1.id and (s.status & 0xf) = 1
  12424.     and    r.keycnt >= 16
  12425.     and    o1.id = c1.id
  12426.     and    c1.colid = r.rkey16
  12427.     and r.fkeyid = o2.id
  12428.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12429.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12430.     and    o2.id = c2.id    
  12431.     and    c2.colid = r.fkey16
  12432.     order by 8,9,2,3,13
  12433.     END
  12434. ELSE IF @fk_table_name is not null
  12435.     BEGIN
  12436.     select
  12437.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12438.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12439.         PK_TABLE_NAME         = o1.name,
  12440.         PK_COLUMN_NAME         = c1.name,
  12441.         PK_COLUMN_GUID        = convert(binary(16),null),
  12442.         PK_COLUMN_PROPID    = convert(int,null),
  12443.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12444.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12445.         FK_TABLE_NAME         = o2.name,
  12446.         FK_COLUMN_NAME         = c2.name,
  12447.         FK_COLUMN_GUID        = convert(binary(16),null),
  12448.         FK_COLUMN_PROPID    = convert(int,null),
  12449.         ORDINAL                = convert(int,1),
  12450.         UPDATE_RULE            = 'NO ACTION',
  12451.         DELETE_RULE         = 'NO ACTION',
  12452.         PK_NAME                = object_name(s.constid),
  12453.         FK_NAME                = object_name(r.constid),
  12454.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12455.     from
  12456.         sysobjects o1, sysobjects o2,
  12457.         syscolumns c1, syscolumns c2,
  12458.         sysreferences r, sysconstraints s
  12459.     where    
  12460.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12461.     and    o2.name = @fk_table_name
  12462.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12463.     and    o2.id = r.fkeyid
  12464.     and    o2.id = c2.id
  12465.     and    c2.colid = r.fkey1
  12466.     and r.rkeyid = o1.id
  12467.     and s.id = o1.id and (s.status & 0xf) = 1
  12468.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12469.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12470.     and    o1.id = c1.id    
  12471.     and    c1.colid = r.rkey1
  12472.     union    all
  12473.     select
  12474.         db_name(r.rkeydbid),
  12475.         user_name(o1.uid),
  12476.         o1.name,
  12477.         c1.name,
  12478.         convert(binary(16),null),
  12479.         convert(int,null),
  12480.         db_name(r.fkeydbid),
  12481.         user_name(o2.uid),
  12482.         o2.name,
  12483.         c2.name,
  12484.         convert(binary(16),null),
  12485.         convert(int,null),
  12486.         convert(int,2),
  12487.         'NO ACTION',
  12488.         'NO ACTION',
  12489.         object_name(s.constid),
  12490.         object_name(r.constid),
  12491.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12492.     from
  12493.         sysobjects o1, sysobjects o2,
  12494.         syscolumns c1, syscolumns c2,
  12495.         sysreferences r, sysconstraints s
  12496.     where    
  12497.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12498.     and    o2.name = @fk_table_name
  12499.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12500.     and    o2.id = r.fkeyid
  12501.     and    r.keycnt >= 2
  12502.     and    o2.id = c2.id
  12503.     and    c2.colid = r.fkey2
  12504.     and r.rkeyid = o1.id
  12505.     and s.id = o1.id and (s.status & 0xf) = 1
  12506.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12507.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12508.     and    o1.id = c1.id    
  12509.     and    c1.colid = r.rkey2
  12510.     union    all
  12511.     select
  12512.         db_name(r.rkeydbid),
  12513.         user_name(o1.uid),
  12514.         o1.name,
  12515.         c1.name,
  12516.         convert(binary(16),null),
  12517.         convert(int,null),
  12518.         db_name(r.fkeydbid),
  12519.         user_name(o2.uid),
  12520.         o2.name,
  12521.         c2.name,
  12522.         convert(binary(16),null),
  12523.         convert(int,null),
  12524.         convert(int,3),
  12525.         'NO ACTION',
  12526.         'NO ACTION',
  12527.         object_name(s.constid),
  12528.         object_name(r.constid),
  12529.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12530.     from
  12531.         sysobjects o1, sysobjects o2,
  12532.         syscolumns c1, syscolumns c2,
  12533.         sysreferences r, sysconstraints s
  12534.     where    
  12535.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12536.     and    o2.name = @fk_table_name
  12537.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12538.     and    o2.id = r.fkeyid
  12539.     and    r.keycnt >= 3
  12540.     and    o2.id = c2.id
  12541.     and    c2.colid = r.fkey3
  12542.     and r.rkeyid = o1.id
  12543.     and s.id = o1.id and (s.status & 0xf) = 1
  12544.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12545.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12546.     and    o1.id = c1.id    
  12547.     and    c1.colid = r.rkey3
  12548.     union    all
  12549.     select
  12550.         db_name(r.rkeydbid),
  12551.         user_name(o1.uid),
  12552.         o1.name,
  12553.         c1.name,
  12554.         convert(binary(16),null),
  12555.         convert(int,null),
  12556.         db_name(r.fkeydbid),
  12557.         user_name(o2.uid),
  12558.         o2.name,
  12559.         c2.name,
  12560.         convert(binary(16),null),
  12561.         convert(int,null),
  12562.         convert(int,4),
  12563.         'NO ACTION',
  12564.         'NO ACTION',
  12565.         object_name(s.constid),
  12566.         object_name(r.constid),
  12567.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12568.     from
  12569.         sysobjects o1, sysobjects o2,
  12570.         syscolumns c1, syscolumns c2,
  12571.         sysreferences r, sysconstraints s
  12572.     where    
  12573.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12574.     and    o2.name = @fk_table_name
  12575.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12576.     and    o2.id = r.fkeyid
  12577.     and    r.keycnt >= 4
  12578.     and    o2.id = c2.id
  12579.     and    c2.colid = r.fkey4
  12580.     and r.rkeyid = o1.id
  12581.     and s.id = o1.id and (s.status & 0xf) = 1
  12582.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12583.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12584.     and    o1.id = c1.id    
  12585.     and    c1.colid = r.rkey4
  12586.     union    all
  12587.     select
  12588.         db_name(r.rkeydbid),
  12589.         user_name(o1.uid),
  12590.         o1.name,
  12591.         c1.name,
  12592.         convert(binary(16),null),
  12593.         convert(int,null),
  12594.         db_name(r.fkeydbid),
  12595.         user_name(o2.uid),
  12596.         o2.name,
  12597.         c2.name,
  12598.         convert(binary(16),null),
  12599.         convert(int,null),
  12600.         convert(int,5),
  12601.         'NO ACTION',
  12602.         'NO ACTION',
  12603.         object_name(s.constid),
  12604.         object_name(r.constid),
  12605.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12606.     from
  12607.         sysobjects o1, sysobjects o2,
  12608.         syscolumns c1, syscolumns c2,
  12609.         sysreferences r, sysconstraints s
  12610.     where    
  12611.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12612.     and    o2.name = @fk_table_name
  12613.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12614.     and    o2.id = r.fkeyid
  12615.     and    r.keycnt >= 5
  12616.     and    o2.id = c2.id
  12617.     and    c2.colid = r.fkey5
  12618.     and r.rkeyid = o1.id
  12619.     and s.id = o1.id and (s.status & 0xf) = 1
  12620.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12621.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12622.     and    o1.id = c1.id    
  12623.     and    c1.colid = r.rkey5
  12624.     union    all
  12625.     select
  12626.         db_name(r.rkeydbid),
  12627.         user_name(o1.uid),
  12628.         o1.name,
  12629.         c1.name,
  12630.         convert(binary(16),null),
  12631.         convert(int,null),
  12632.         db_name(r.fkeydbid),
  12633.         user_name(o2.uid),
  12634.         o2.name,
  12635.         c2.name,
  12636.         convert(binary(16),null),
  12637.         convert(int,null),
  12638.         convert(int,6),
  12639.         'NO ACTION',
  12640.         'NO ACTION',
  12641.         object_name(s.constid),
  12642.         object_name(r.constid),
  12643.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  12644.     from
  12645.         sysobjects o1, sysobjects o2,
  12646.         syscolumns c1, syscolumns c2,
  12647.         sysreferences r, sysconstraints s
  12648.     where    
  12649.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12650.     and    o2.name = @fk_table_name
  12651.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12652.     and    o2.id = r.fkeyid
  12653.     and    r.keycnt >= 6
  12654.     and    o2.id = c2.id
  12655.     and    c2.colid = r.fkey6
  12656.     and r.rkeyid = o1.id
  12657.     and s.id = o1.id and (s.status & 0xf) = 1
  12658.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12659.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12660.     and    o1.id = c1.id    
  12661.     and    c1.colid = r.rkey6
  12662.     union    all
  12663.     select
  12664.         db_name(r.rkeydbid),
  12665.         user_name(o1.uid),
  12666.         o1.name,
  12667.         c1.name,
  12668.         convert(binary(16),null),
  12669.         convert(int,null),
  12670.         db_name(r.fkeydbid),
  12671.         user_name(o2.uid),
  12672.         o2.name,
  12673.         c2.name,
  12674.         convert(binary(16),null),
  12675.         convert(int,null),
  12676.         convert(int,7),
  12677.         'NO ACTION',
  12678.         'NO ACTION',
  12679.         object_name(s.constid),
  12680.         object_name(r.constid),
  12681.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12682.     from
  12683.         sysobjects o1, sysobjects o2,
  12684.         syscolumns c1, syscolumns c2,
  12685.         sysreferences r, sysconstraints s
  12686.     where    
  12687.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12688.     and    o2.name = @fk_table_name
  12689.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12690.     and    o2.id = r.fkeyid
  12691.     and    r.keycnt >= 7
  12692.     and    o2.id = c2.id
  12693.     and    c2.colid = r.fkey7
  12694.     and    r.rkeyid = o1.id
  12695.     and s.id = o1.id and (s.status & 0xf) = 1
  12696.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12697.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12698.     and    o1.id = c1.id    
  12699.     and    c1.colid = r.rkey7
  12700.     union    all
  12701.     select
  12702.         db_name(r.rkeydbid),
  12703.         user_name(o1.uid),
  12704.         o1.name,
  12705.         c1.name,
  12706.         convert(binary(16),null),
  12707.         convert(int,null),
  12708.         db_name(r.fkeydbid),
  12709.         user_name(o2.uid),
  12710.         o2.name,
  12711.         c2.name,
  12712.         convert(binary(16),null),
  12713.         convert(int,null),
  12714.         convert(int,8),
  12715.         'NO ACTION',
  12716.         'NO ACTION',
  12717.         object_name(s.constid),
  12718.         object_name(r.constid),
  12719.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12720.     from
  12721.         sysobjects o1, sysobjects o2,
  12722.         syscolumns c1, syscolumns c2,
  12723.         sysreferences r, sysconstraints s
  12724.     where    
  12725.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12726.     and    o2.name = @fk_table_name
  12727.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12728.     and    o2.id = r.fkeyid
  12729.     and    r.keycnt >= 8
  12730.     and    o2.id = c2.id
  12731.     and    c2.colid = r.fkey8
  12732.     and r.rkeyid = o1.id
  12733.     and s.id = o1.id and (s.status & 0xf) = 1
  12734.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12735.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12736.     and    o1.id = c1.id    
  12737.     and    c1.colid = r.rkey8
  12738.     union    all
  12739.     select
  12740.         db_name(r.rkeydbid),
  12741.         user_name(o1.uid),
  12742.         o1.name,
  12743.         c1.name,
  12744.         convert(binary(16),null),
  12745.         convert(int,null),
  12746.         db_name(r.fkeydbid),
  12747.         user_name(o2.uid),
  12748.         o2.name,
  12749.         c2.name,
  12750.         convert(binary(16),null),
  12751.         convert(int,null),
  12752.         convert(int,9),
  12753.         'NO ACTION',
  12754.         'NO ACTION',
  12755.         object_name(s.constid),
  12756.         object_name(r.constid),
  12757.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12758.     from
  12759.         sysobjects o1, sysobjects o2,
  12760.         syscolumns c1, syscolumns c2,
  12761.         sysreferences r, sysconstraints s
  12762.     where    
  12763.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12764.     and    o2.name = @fk_table_name
  12765.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12766.     and    o2.id = r.fkeyid
  12767.     and    r.keycnt >= 9
  12768.     and    o2.id = c2.id
  12769.     and    c2.colid = r.fkey9
  12770.     and r.rkeyid = o1.id
  12771.     and s.id = o1.id and (s.status & 0xf) = 1
  12772.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12773.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12774.     and    o1.id = c1.id    
  12775.     and    c1.colid = r.rkey9
  12776.     union    all
  12777.     select
  12778.         db_name(r.rkeydbid),
  12779.         user_name(o1.uid),
  12780.         o1.name,
  12781.         c1.name,
  12782.         convert(binary(16),null),
  12783.         convert(int,null),
  12784.         db_name(r.fkeydbid),
  12785.         user_name(o2.uid),
  12786.         o2.name,
  12787.         c2.name,
  12788.         convert(binary(16),null),
  12789.         convert(int,null),
  12790.         convert(int,10),
  12791.         'NO ACTION',
  12792.         'NO ACTION',
  12793.         object_name(s.constid),
  12794.         object_name(r.constid),
  12795.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12796.     from
  12797.         sysobjects o1, sysobjects o2,
  12798.         syscolumns c1, syscolumns c2,
  12799.         sysreferences r, sysconstraints s
  12800.     where    
  12801.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12802.     and    o2.name = @fk_table_name
  12803.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12804.     and    o2.id = r.fkeyid
  12805.     and    r.keycnt >= 10
  12806.     and    o2.id = c2.id
  12807.     and    c2.colid = r.fkey10
  12808.     and r.rkeyid = o1.id
  12809.     and s.id = o1.id and (s.status & 0xf) = 1
  12810.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12811.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12812.     and    o1.id = c1.id    
  12813.     and    c1.colid = r.rkey10
  12814.     union    all
  12815.     select
  12816.         db_name(r.rkeydbid),
  12817.         user_name(o1.uid),
  12818.         o1.name,
  12819.         c1.name,
  12820.         convert(binary(16),null),
  12821.         convert(int,null),
  12822.         db_name(r.fkeydbid),
  12823.         user_name(o2.uid),
  12824.         o2.name,
  12825.         c2.name,
  12826.         convert(binary(16),null),
  12827.         convert(int,null),
  12828.         convert(int,11),
  12829.         'NO ACTION',
  12830.         'NO ACTION',
  12831.         object_name(s.constid),
  12832.         object_name(r.constid),
  12833.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12834.     from
  12835.         sysobjects o1, sysobjects o2,
  12836.         syscolumns c1, syscolumns c2,
  12837.         sysreferences r, sysconstraints s
  12838.     where    
  12839.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12840.     and    o2.name = @fk_table_name
  12841.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12842.     and    o2.id = r.fkeyid
  12843.     and    r.keycnt >= 11
  12844.     and    o2.id = c2.id
  12845.     and    c2.colid = r.fkey11
  12846.     and r.rkeyid = o1.id
  12847.     and s.id = o1.id and (s.status & 0xf) = 1
  12848.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12849.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12850.     and    o1.id = c1.id    
  12851.     and    c1.colid = r.rkey11
  12852.     union    all
  12853.     select
  12854.         db_name(r.rkeydbid),
  12855.         user_name(o1.uid),
  12856.         o1.name,
  12857.         c1.name,
  12858.         convert(binary(16),null),
  12859.         convert(int,null),
  12860.         db_name(r.fkeydbid),
  12861.         user_name(o2.uid),
  12862.         o2.name,
  12863.         c2.name,
  12864.         convert(binary(16),null),
  12865.         convert(int,null),
  12866.         convert(int,12),
  12867.         'NO ACTION',
  12868.         'NO ACTION',
  12869.         object_name(s.constid),
  12870.         object_name(r.constid),
  12871.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12872.     from
  12873.         sysobjects o1, sysobjects o2,
  12874.         syscolumns c1, syscolumns c2,
  12875.         sysreferences r, sysconstraints s
  12876.     where    
  12877.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12878.     and    o2.name = @fk_table_name
  12879.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12880.     and    o2.id = r.fkeyid
  12881.     and    r.keycnt >= 12
  12882.     and    o2.id = c2.id
  12883.     and    c2.colid = r.fkey12
  12884.     and r.rkeyid = o1.id
  12885.     and s.id = o1.id and (s.status & 0xf) = 1
  12886.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12887.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12888.     and    o1.id = c1.id    
  12889.     and    c1.colid = r.rkey12
  12890.     union    all
  12891.     select
  12892.         db_name(r.rkeydbid),
  12893.         user_name(o1.uid),
  12894.         o1.name,
  12895.         c1.name,
  12896.         convert(binary(16),null),
  12897.         convert(int,null),
  12898.         db_name(r.fkeydbid),
  12899.         user_name(o2.uid),
  12900.         o2.name,
  12901.         c2.name,
  12902.         convert(binary(16),null),
  12903.         convert(int,null),
  12904.         convert(int,13),
  12905.         'NO ACTION',
  12906.         'NO ACTION',
  12907.         object_name(s.constid),
  12908.         object_name(r.constid),
  12909.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12910.     from
  12911.         sysobjects o1, sysobjects o2,
  12912.         syscolumns c1, syscolumns c2,
  12913.         sysreferences r, sysconstraints s
  12914.     where    
  12915.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12916.     and    o2.name = @fk_table_name
  12917.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12918.     and    o2.id = r.fkeyid
  12919.     and    r.keycnt >= 13
  12920.     and    o2.id = c2.id
  12921.     and    c2.colid = r.fkey13
  12922.     and r.rkeyid = o1.id
  12923.     and s.id = o1.id and (s.status & 0xf) = 1
  12924.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12925.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12926.     and    o1.id = c1.id    
  12927.     and    c1.colid = r.rkey13
  12928.     union    all
  12929.     select
  12930.         db_name(r.rkeydbid),
  12931.         user_name(o1.uid),
  12932.         o1.name,
  12933.         c1.name,
  12934.         convert(binary(16),null),
  12935.         convert(int,null),
  12936.         db_name(r.fkeydbid),
  12937.         user_name(o2.uid),
  12938.         o2.name,
  12939.         c2.name,
  12940.         convert(binary(16),null),
  12941.         convert(int,null),
  12942.         convert(int,14),
  12943.         'NO ACTION',
  12944.         'NO ACTION',
  12945.         object_name(s.constid),
  12946.         object_name(r.constid),
  12947.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12948.     from
  12949.         sysobjects o1, sysobjects o2,
  12950.         syscolumns c1, syscolumns c2,
  12951.         sysreferences r, sysconstraints s
  12952.     where    
  12953.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12954.     and    o2.name = @fk_table_name
  12955.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12956.     and    o2.id = r.fkeyid
  12957.     and    r.keycnt >= 14
  12958.     and    o2.id = c2.id
  12959.     and    c2.colid = r.fkey14
  12960.     and    r.rkeyid = o1.id
  12961.     and s.id = o1.id and (s.status & 0xf) = 1
  12962.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12963.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12964.     and    o1.id = c1.id    
  12965.     and    c1.colid = r.rkey14
  12966.     union    all
  12967.     select
  12968.         db_name(r.rkeydbid),
  12969.         user_name(o1.uid),
  12970.         o1.name,
  12971.         c1.name,
  12972.         convert(binary(16),null),
  12973.         convert(int,null),
  12974.         db_name(r.fkeydbid),
  12975.         user_name(o2.uid),
  12976.         o2.name,
  12977.         c2.name,
  12978.         convert(binary(16),null),
  12979.         convert(int,null),
  12980.         convert(int,15),
  12981.         'NO ACTION',
  12982.         'NO ACTION',
  12983.         object_name(s.constid),
  12984.         object_name(r.constid),
  12985.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12986.     from
  12987.         sysobjects o1, sysobjects o2,
  12988.         syscolumns c1, syscolumns c2,
  12989.         sysreferences r, sysconstraints s
  12990.     where    
  12991.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12992.     and    o2.name = @fk_table_name
  12993.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12994.     and    o2.id = r.fkeyid
  12995.     and    r.keycnt >= 15
  12996.     and    o2.id = c2.id
  12997.     and    c2.colid = r.fkey15
  12998.     and r.rkeyid = o1.id
  12999.     and s.id = o1.id and (s.status & 0xf) = 1
  13000.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13001.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13002.     and    o1.id = c1.id    
  13003.     and    c1.colid = r.rkey15
  13004.     union    all
  13005.     select
  13006.         db_name(r.rkeydbid),
  13007.         user_name(o1.uid),
  13008.         o1.name,
  13009.         c1.name,
  13010.         convert(binary(16),null),
  13011.         convert(int,null),
  13012.         db_name(r.fkeydbid),
  13013.         user_name(o2.uid),
  13014.         o2.name,
  13015.         c2.name,
  13016.         convert(binary(16),null),
  13017.         convert(int,null),
  13018.         convert(int,16),
  13019.         'NO ACTION',
  13020.         'NO ACTION',
  13021.         object_name(s.constid),
  13022.         object_name(r.constid),
  13023.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13024.     from
  13025.         sysobjects o1, sysobjects o2,
  13026.         syscolumns c1, syscolumns c2,
  13027.         sysreferences r, sysconstraints s
  13028.     where    
  13029.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13030.     and    o2.name = @fk_table_name
  13031.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13032.     and    o2.id = r.fkeyid
  13033.     and    r.keycnt >= 16
  13034.     and    o2.id = c2.id
  13035.     and    c2.colid = r.fkey16
  13036.     and r.rkeyid = o1.id
  13037.     and s.id = o1.id and (s.status & 0xf) = 1
  13038.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13039.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13040.     and    o1.id = c1.id    
  13041.     and    c1.colid = r.rkey16
  13042.     order by 8,9,2,3,13
  13043.     END
  13044. ELSE
  13045.     BEGIN
  13046.     select
  13047.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13048.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13049.         PK_TABLE_NAME         = o1.name,
  13050.         PK_COLUMN_NAME         = c1.name,
  13051.         PK_COLUMN_GUID        = convert(binary(16),null),
  13052.         PK_COLUMN_PROPID    = convert(int,null),
  13053.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13054.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13055.         FK_TABLE_NAME         = o2.name,
  13056.         FK_COLUMN_NAME         = c2.name,
  13057.         FK_COLUMN_GUID        = convert(binary(16),null),
  13058.         FK_COLUMN_PROPID    = convert(int,null),
  13059.         ORDINAL                = convert(int,1),
  13060.         UPDATE_RULE            = 'NO ACTION',
  13061.         DELETE_RULE         = 'NO ACTION',
  13062.         PK_NAME                = object_name(s.constid),
  13063.         FK_NAME                = object_name(r.constid),
  13064.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13065.     from
  13066.         sysobjects o1, sysobjects o2,
  13067.         syscolumns c1, syscolumns c2,
  13068.         sysreferences r, sysconstraints s
  13069.     where    
  13070.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13071.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13072.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13073.     and    o1.id = r.rkeyid
  13074.     and s.id = o1.id and (s.status & 0xf) = 1
  13075.     and    o1.id = c1.id
  13076.     and    c1.colid = r.rkey1
  13077.     and r.fkeyid = o2.id
  13078.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13079.     and    o2.id = c2.id    
  13080.     and    c2.colid = r.fkey1
  13081.     union all
  13082.     select
  13083.         db_name(r.rkeydbid),
  13084.         user_name(o1.uid),
  13085.         o1.name,
  13086.         c1.name,
  13087.         convert(binary(16),null),
  13088.         convert(int,null),
  13089.         db_name(r.fkeydbid),
  13090.         user_name(o2.uid),
  13091.         o2.name,
  13092.         c2.name,
  13093.         convert(binary(16),null),
  13094.         convert(int,null),
  13095.         convert(int,2),
  13096.         'NO ACTION',
  13097.         'NO ACTION',
  13098.         object_name(s.constid),
  13099.         object_name(r.constid),
  13100.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13101.     from
  13102.         sysobjects o1, sysobjects o2,
  13103.         syscolumns c1, syscolumns c2,
  13104.         sysreferences r, sysconstraints s
  13105.     where    
  13106.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13107.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13108.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13109.     and    o1.id = r.rkeyid
  13110.     and s.id = o1.id and (s.status & 0xf) = 1
  13111.     and    r.keycnt >= 2
  13112.     and    o1.id = c1.id
  13113.     and    c1.colid = r.rkey2
  13114.     and r.fkeyid = o2.id
  13115.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13116.     and    o2.id = c2.id    
  13117.     and    c2.colid = r.fkey2
  13118.     union all
  13119.     select
  13120.         db_name(r.rkeydbid),
  13121.         user_name(o1.uid),
  13122.         o1.name,
  13123.         c1.name,
  13124.         convert(binary(16),null),
  13125.         convert(int,null),
  13126.         db_name(r.fkeydbid),
  13127.         user_name(o2.uid),
  13128.         o2.name,
  13129.         c2.name,
  13130.         convert(binary(16),null),
  13131.         convert(int,null),
  13132.         convert(int,3),
  13133.         'NO ACTION',
  13134.         'NO ACTION',
  13135.         object_name(s.constid),
  13136.         object_name(r.constid),
  13137.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13138.     from
  13139.         sysobjects o1, sysobjects o2,
  13140.         syscolumns c1, syscolumns c2,
  13141.         sysreferences r, sysconstraints s
  13142.     where    
  13143.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13144.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13145.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13146.     and    o1.id = r.rkeyid
  13147.     and s.id = o1.id and (s.status & 0xf) = 1
  13148.     and    r.keycnt >= 3
  13149.     and    o1.id = c1.id
  13150.     and    c1.colid = r.rkey3
  13151.     and r.fkeyid = o2.id
  13152.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13153.     and    o2.id = c2.id    
  13154.     and    c2.colid = r.fkey3
  13155.     union all
  13156.     select
  13157.         db_name(r.rkeydbid),
  13158.         user_name(o1.uid),
  13159.         o1.name,
  13160.         c1.name,
  13161.         convert(binary(16),null),
  13162.         convert(int,null),
  13163.         db_name(r.fkeydbid),
  13164.         user_name(o2.uid),
  13165.         o2.name,
  13166.         c2.name,
  13167.         convert(binary(16),null),
  13168.         convert(int,null),
  13169.         convert(int,4),
  13170.         'NO ACTION',
  13171.         'NO ACTION',
  13172.         object_name(s.constid),
  13173.         object_name(r.constid),
  13174.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13175.     from
  13176.         sysobjects o1, sysobjects o2,
  13177.         syscolumns c1, syscolumns c2,
  13178.         sysreferences r, sysconstraints s
  13179.     where    
  13180.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13181.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13182.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13183.     and    o1.id = r.rkeyid
  13184.     and s.id = o1.id and (s.status & 0xf) = 1
  13185.     and    r.keycnt >= 4
  13186.     and    o1.id = c1.id
  13187.     and    c1.colid = r.rkey4
  13188.     and r.fkeyid = o2.id
  13189.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13190.     and    o2.id = c2.id    
  13191.     and    c2.colid = r.fkey4
  13192.     union all
  13193.     select
  13194.         db_name(r.rkeydbid),
  13195.         user_name(o1.uid),
  13196.         o1.name,
  13197.         c1.name,
  13198.         convert(binary(16),null),
  13199.         convert(int,null),
  13200.         db_name(r.fkeydbid),
  13201.         user_name(o2.uid),
  13202.         o2.name,
  13203.         c2.name,
  13204.         convert(binary(16),null),
  13205.         convert(int,null),
  13206.         convert(int,5),
  13207.         'NO ACTION',
  13208.         'NO ACTION',
  13209.         object_name(s.constid),
  13210.         object_name(r.constid),
  13211.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13212.     from
  13213.         sysobjects o1, sysobjects o2,
  13214.         syscolumns c1, syscolumns c2,
  13215.         sysreferences r, sysconstraints s
  13216.     where    
  13217.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13218.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13219.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13220.     and    o1.id = r.rkeyid
  13221.     and s.id = o1.id and (s.status & 0xf) = 1
  13222.     and    r.keycnt >= 5
  13223.     and    o1.id = c1.id
  13224.     and    c1.colid = r.rkey5
  13225.     and r.fkeyid = o2.id
  13226.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13227.     and    o2.id = c2.id    
  13228.     and    c2.colid = r.fkey5
  13229.     union all
  13230.     select
  13231.         db_name(r.rkeydbid),
  13232.         user_name(o1.uid),
  13233.         o1.name,
  13234.         c1.name,
  13235.         convert(binary(16),null),
  13236.         convert(int,null),
  13237.         db_name(r.fkeydbid),
  13238.         user_name(o2.uid),
  13239.         o2.name,
  13240.         c2.name,
  13241.         convert(binary(16),null),
  13242.         convert(int,null),
  13243.         convert(int,6),
  13244.         'NO ACTION',
  13245.         'NO ACTION',
  13246.         object_name(s.constid),
  13247.         object_name(r.constid),
  13248.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13249.     from
  13250.         sysobjects o1, sysobjects o2,
  13251.         syscolumns c1, syscolumns c2,
  13252.         sysreferences r, sysconstraints s
  13253.     where    
  13254.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13255.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13256.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13257.     and    o1.id = r.rkeyid
  13258.     and s.id = o1.id and (s.status & 0xf) = 1
  13259.     and    r.keycnt >= 6
  13260.     and    o1.id = c1.id
  13261.     and    c1.colid = r.rkey6
  13262.     and r.fkeyid = o2.id
  13263.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13264.     and    o2.id = c2.id    
  13265.     and    c2.colid = r.fkey6
  13266.     union all
  13267.     select
  13268.         db_name(r.rkeydbid),
  13269.         user_name(o1.uid),
  13270.         o1.name,
  13271.         c1.name,
  13272.         convert(binary(16),null),
  13273.         convert(int,null),
  13274.         db_name(r.fkeydbid),
  13275.         user_name(o2.uid),
  13276.         o2.name,
  13277.         c2.name,
  13278.         convert(binary(16),null),
  13279.         convert(int,null),
  13280.         convert(int,7),
  13281.         'NO ACTION',
  13282.         'NO ACTION',
  13283.         object_name(s.constid),
  13284.         object_name(r.constid),
  13285.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13286.     from
  13287.         sysobjects o1, sysobjects o2,
  13288.         syscolumns c1, syscolumns c2,
  13289.         sysreferences r, sysconstraints s
  13290.     where    
  13291.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13292.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13293.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13294.     and    o1.id = r.rkeyid
  13295.     and s.id = o1.id and (s.status & 0xf) = 1
  13296.     and    r.keycnt >= 7
  13297.     and    o1.id = c1.id
  13298.     and    c1.colid = r.rkey7
  13299.     and r.fkeyid = o2.id
  13300.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13301.     and    o2.id = c2.id    
  13302.     and    c2.colid = r.fkey7
  13303.     union all
  13304.     select
  13305.         db_name(r.rkeydbid),
  13306.         user_name(o1.uid),
  13307.         o1.name,
  13308.         c1.name,
  13309.         convert(binary(16),null),
  13310.         convert(int,null),
  13311.         db_name(r.fkeydbid),
  13312.         user_name(o2.uid),
  13313.         o2.name,
  13314.         c2.name,
  13315.         convert(binary(16),null),
  13316.         convert(int,null),
  13317.         convert(int,8),
  13318.         'NO ACTION',
  13319.         'NO ACTION',
  13320.         object_name(s.constid),
  13321.         object_name(r.constid),
  13322.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13323.     from
  13324.         sysobjects o1, sysobjects o2,
  13325.         syscolumns c1, syscolumns c2,
  13326.         sysreferences r, sysconstraints s
  13327.     where    
  13328.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13329.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13330.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13331.     and    o1.id = r.rkeyid
  13332.     and s.id = o1.id and (s.status & 0xf) = 1
  13333.     and    r.keycnt >= 8
  13334.     and    o1.id = c1.id
  13335.     and    c1.colid = r.rkey8
  13336.     and r.fkeyid = o2.id
  13337.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13338.     and    o2.id = c2.id    
  13339.     and    c2.colid = r.fkey8
  13340.     union all
  13341.     select
  13342.         db_name(r.rkeydbid),
  13343.         user_name(o1.uid),
  13344.         o1.name,
  13345.         c1.name,
  13346.         convert(binary(16),null),
  13347.         convert(int,null),
  13348.         db_name(r.fkeydbid),
  13349.         user_name(o2.uid),
  13350.         o2.name,
  13351.         c2.name,
  13352.         convert(binary(16),null),
  13353.         convert(int,null),
  13354.         convert(int,9),
  13355.         'NO ACTION',
  13356.         'NO ACTION',
  13357.         object_name(s.constid),
  13358.         object_name(r.constid),
  13359.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13360.     from
  13361.         sysobjects o1, sysobjects o2,
  13362.         syscolumns c1, syscolumns c2,
  13363.         sysreferences r, sysconstraints s
  13364.     where    
  13365.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13366.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13367.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13368.     and    o1.id = r.rkeyid
  13369.     and s.id = o1.id and (s.status & 0xf) = 1
  13370.     and    r.keycnt >= 9
  13371.     and    o1.id = c1.id
  13372.     and    c1.colid = r.rkey9
  13373.     and r.fkeyid = o2.id
  13374.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13375.     and    o2.id = c2.id    
  13376.     and    c2.colid = r.fkey9
  13377.     union all
  13378.     select
  13379.         db_name(r.rkeydbid),
  13380.         user_name(o1.uid),
  13381.         o1.name,
  13382.         c1.name,
  13383.         convert(binary(16),null),
  13384.         convert(int,null),
  13385.         db_name(r.fkeydbid),
  13386.         user_name(o2.uid),
  13387.         o2.name,
  13388.         c2.name,
  13389.         convert(binary(16),null),
  13390.         convert(int,null),
  13391.         convert(int,10),
  13392.         'NO ACTION',
  13393.         'NO ACTION',
  13394.         object_name(s.constid),
  13395.         object_name(r.constid),
  13396.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13397.     from
  13398.         sysobjects o1, sysobjects o2,
  13399.         syscolumns c1, syscolumns c2,
  13400.         sysreferences r, sysconstraints s
  13401.     where    
  13402.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13403.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13404.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13405.     and    o1.id = r.rkeyid
  13406.     and s.id = o1.id and (s.status & 0xf) = 1
  13407.     and    r.keycnt >= 10
  13408.     and    o1.id = c1.id
  13409.     and    c1.colid = r.rkey10
  13410.     and r.fkeyid = o2.id
  13411.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13412.     and    o2.id = c2.id    
  13413.     and    c2.colid = r.fkey10
  13414.     union all
  13415.     select
  13416.         db_name(r.rkeydbid),
  13417.         user_name(o1.uid),
  13418.         o1.name,
  13419.         c1.name,
  13420.         convert(binary(16),null),
  13421.         convert(int,null),
  13422.         db_name(r.fkeydbid),
  13423.         user_name(o2.uid),
  13424.         o2.name,
  13425.         c2.name,
  13426.         convert(binary(16),null),
  13427.         convert(int,null),
  13428.         convert(int,11),
  13429.         'NO ACTION',
  13430.         'NO ACTION',
  13431.         object_name(s.constid),
  13432.         object_name(r.constid),
  13433.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13434.     from
  13435.         sysobjects o1, sysobjects o2,
  13436.         syscolumns c1, syscolumns c2,
  13437.         sysreferences r, sysconstraints s
  13438.     where    
  13439.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13440.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13441.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13442.     and    o1.id = r.rkeyid
  13443.     and s.id = o1.id and (s.status & 0xf) = 1
  13444.     and    r.keycnt >= 11
  13445.     and    o1.id = c1.id
  13446.     and    c1.colid = r.rkey11
  13447.     and r.fkeyid = o2.id
  13448.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13449.     and    o2.id = c2.id    
  13450.     and    c2.colid = r.fkey11
  13451.     union all
  13452.     select
  13453.         db_name(r.rkeydbid),
  13454.         user_name(o1.uid),
  13455.         o1.name,
  13456.         c1.name,
  13457.         convert(binary(16),null),
  13458.         convert(int,null),
  13459.         db_name(r.fkeydbid),
  13460.         user_name(o2.uid),
  13461.         o2.name,
  13462.         c2.name,
  13463.         convert(binary(16),null),
  13464.         convert(int,null),
  13465.         convert(int,12),
  13466.         'NO ACTION',
  13467.         'NO ACTION',
  13468.         object_name(s.constid),
  13469.         object_name(r.constid),
  13470.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13471.     from
  13472.         sysobjects o1, sysobjects o2,
  13473.         syscolumns c1, syscolumns c2,
  13474.         sysreferences r, sysconstraints s
  13475.     where    
  13476.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13477.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13478.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13479.     and    o1.id = r.rkeyid
  13480.     and s.id = o1.id and (s.status & 0xf) = 1
  13481.     and    r.keycnt >= 12
  13482.     and    o1.id = c1.id
  13483.     and    c1.colid = r.rkey12
  13484.     and r.fkeyid = o2.id
  13485.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13486.     and    o2.id = c2.id    
  13487.     and    c2.colid = r.fkey12
  13488.     union all
  13489.     select
  13490.         db_name(r.rkeydbid),
  13491.         user_name(o1.uid),
  13492.         o1.name,
  13493.         c1.name,
  13494.         convert(binary(16),null),
  13495.         convert(int,null),
  13496.         db_name(r.fkeydbid),
  13497.         user_name(o2.uid),
  13498.         o2.name,
  13499.         c2.name,
  13500.         convert(binary(16),null),
  13501.         convert(int,null),
  13502.         convert(int,13),
  13503.         'NO ACTION',
  13504.         'NO ACTION',
  13505.         object_name(s.constid),
  13506.         object_name(r.constid),
  13507.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13508.     from
  13509.         sysobjects o1, sysobjects o2,
  13510.         syscolumns c1, syscolumns c2,
  13511.         sysreferences r, sysconstraints s
  13512.     where    
  13513.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13514.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13515.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13516.     and    o1.id = r.rkeyid
  13517.     and s.id = o1.id and (s.status & 0xf) = 1
  13518.     and    r.keycnt >= 13
  13519.     and    o1.id = c1.id
  13520.     and    c1.colid = r.rkey13
  13521.     and r.fkeyid = o2.id
  13522.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13523.     and    o2.id = c2.id    
  13524.     and    c2.colid = r.fkey13
  13525.     union all
  13526.     select
  13527.         db_name(r.rkeydbid),
  13528.         user_name(o1.uid),
  13529.         o1.name,
  13530.         c1.name,
  13531.         convert(binary(16),null),
  13532.         convert(int,null),
  13533.         db_name(r.fkeydbid),
  13534.         user_name(o2.uid),
  13535.         o2.name,
  13536.         c2.name,
  13537.         convert(binary(16),null),
  13538.         convert(int,null),
  13539.         convert(int,14),
  13540.         'NO ACTION',
  13541.         'NO ACTION',
  13542.         object_name(s.constid),
  13543.         object_name(r.constid),
  13544.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13545.     from
  13546.         sysobjects o1, sysobjects o2,
  13547.         syscolumns c1, syscolumns c2,
  13548.         sysreferences r, sysconstraints s
  13549.     where    
  13550.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13551.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13552.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13553.     and    o1.id = r.rkeyid
  13554.     and s.id = o1.id and (s.status & 0xf) = 1
  13555.     and    r.keycnt >= 14
  13556.     and    o1.id = c1.id
  13557.     and    c1.colid = r.rkey14
  13558.     and r.fkeyid = o2.id
  13559.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13560.     and    o2.id = c2.id    
  13561.     and    c2.colid = r.fkey14
  13562.     union all
  13563.     select
  13564.         db_name(r.rkeydbid),
  13565.         user_name(o1.uid),
  13566.         o1.name,
  13567.         c1.name,
  13568.         convert(binary(16),null),
  13569.         convert(int,null),
  13570.         db_name(r.fkeydbid),
  13571.         user_name(o2.uid),
  13572.         o2.name,
  13573.         c2.name,
  13574.         convert(binary(16),null),
  13575.         convert(int,null),
  13576.         convert(int,15),
  13577.         'NO ACTION',
  13578.         'NO ACTION',
  13579.         object_name(s.constid),
  13580.         object_name(r.constid),
  13581.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13582.     from
  13583.         sysobjects o1, sysobjects o2,
  13584.         syscolumns c1, syscolumns c2,
  13585.         sysreferences r, sysconstraints s
  13586.     where    
  13587.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13588.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13589.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13590.     and    o1.id = r.rkeyid
  13591.     and s.id = o1.id and (s.status & 0xf) = 1
  13592.     and    r.keycnt >= 15
  13593.     and    o1.id = c1.id
  13594.     and    c1.colid = r.rkey15
  13595.     and r.fkeyid = o2.id
  13596.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13597.     and    o2.id = c2.id    
  13598.     and    c2.colid = r.fkey15
  13599.     union all
  13600.     select
  13601.         db_name(r.rkeydbid),
  13602.         user_name(o1.uid),
  13603.         o1.name,
  13604.         c1.name,
  13605.         convert(binary(16),null),
  13606.         convert(int,null),
  13607.         db_name(r.fkeydbid),
  13608.         user_name(o2.uid),
  13609.         o2.name,
  13610.         c2.name,
  13611.         convert(binary(16),null),
  13612.         convert(int,null),
  13613.         convert(int,16),
  13614.         'NO ACTION',
  13615.         'NO ACTION',
  13616.         object_name(s.constid),
  13617.         object_name(r.constid),
  13618.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13619.     from
  13620.         sysobjects o1, sysobjects o2,
  13621.         syscolumns c1, syscolumns c2,
  13622.         sysreferences r, sysconstraints s
  13623.     where    
  13624.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13625.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13626.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13627.     and    o1.id = r.rkeyid
  13628.     and s.id = o1.id and (s.status & 0xf) = 1
  13629.     and    o1.id = c1.id
  13630.     and    r.keycnt >= 16
  13631.     and    c1.colid = r.rkey16
  13632.     and r.fkeyid = o2.id
  13633.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13634.     and    o2.id = c2.id    
  13635.     and    c2.colid = r.fkey16
  13636.     order by 8,9,2,3,13
  13637.     END
  13638. go
  13639. dump tran master with no_log
  13640. go
  13641. CREATE PROCEDURE sp_foreign_keys_rowset;2
  13642.     (
  13643.        @handle            int output,
  13644.        @scrollopt        int output,
  13645.     @ccopt            int output,
  13646.     @rows            int output,
  13647.      @pk_table_name        varchar(255) = null,
  13648.     @pk_table_schema    varchar(255) = null,
  13649.     @pk_table_catalog    varchar(255) = null,
  13650.     @fk_table_name        varchar(255) = null,
  13651.     @fk_table_schema    varchar(255) = null,
  13652.     @fk_table_catalog    varchar(255) = null
  13653.     )
  13654. as
  13655.     declare @ret int
  13656.     
  13657.     SET NOCOUNT ON
  13658.  
  13659.     create table #spfkeysrowset1
  13660.         (
  13661.         PK_TABLE_CATALOG    sysname not null,
  13662.         PK_TABLE_SCHEMA        sysname not null,
  13663.         PK_TABLE_NAME         sysname not null,
  13664.         PK_COLUMN_NAME         sysname not null,
  13665.         PK_COLUMN_GUID        binary(16) null,
  13666.         PK_COLUMN_PROPID    int null,
  13667.         FK_TABLE_CATALOG    sysname not null,
  13668.         FK_TABLE_SCHEMA        sysname not null,
  13669.         FK_TABLE_NAME         sysname not null,
  13670.         FK_COLUMN_NAME         sysname not null,
  13671.         FK_COLUMN_GUID        binary(16) null,
  13672.         FK_COLUMN_PROPID    int null,
  13673.         ORDINAL                int not null,
  13674.         UPDATE_RULE            sysname not null,
  13675.         DELETE_RULE         sysname not null,
  13676.         PK_NAME                sysname not null,
  13677.         FK_NAME                sysname not null,
  13678.         DEFERRABILITY        smallint not null        
  13679.         )
  13680.  
  13681. IF @pk_table_name is not null
  13682.     BEGIN
  13683.     insert into #spfkeysrowset1
  13684.     select
  13685.         db_name(r.rkeydbid),
  13686.         user_name(o1.uid),
  13687.         o1.name,
  13688.         c1.name,
  13689.         convert(binary(16),null),
  13690.         convert(int,null),
  13691.         db_name(r.fkeydbid),
  13692.         user_name(o2.uid),
  13693.         o2.name,
  13694.         c2.name,
  13695.         convert(binary(16),null),
  13696.         convert(int,null),
  13697.         convert(int,1),
  13698.         'NO ACTION',
  13699.         'NO ACTION',
  13700.         object_name(s.constid),
  13701.         object_name(r.constid),
  13702.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13703.     from
  13704.         sysobjects o1, sysobjects o2,
  13705.         syscolumns c1, syscolumns c2,
  13706.         sysreferences r, sysconstraints s
  13707.     where    
  13708.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13709.     and    o1.name = @pk_table_name
  13710.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13711.     and    o1.id = r.rkeyid
  13712.     and s.id = o1.id and (s.status & 0xf) = 1
  13713.     and    o1.id = c1.id
  13714.     and    c1.colid = r.rkey1
  13715.     and r.fkeyid = o2.id
  13716.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13717.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13718.     and    o2.id = c2.id    
  13719.     and    c2.colid = r.fkey1
  13720.     union all
  13721.     select
  13722.         db_name(r.rkeydbid),
  13723.         user_name(o1.uid),
  13724.         o1.name,
  13725.         c1.name,
  13726.         convert(binary(16),null),
  13727.         convert(int,null),
  13728.         db_name(r.fkeydbid),
  13729.         user_name(o2.uid),
  13730.         o2.name,
  13731.         c2.name,
  13732.         convert(binary(16),null),
  13733.         convert(int,null),
  13734.         convert(int,2),
  13735.         'NO ACTION',
  13736.         'NO ACTION',
  13737.         object_name(s.constid),
  13738.         object_name(r.constid),
  13739.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13740.     from
  13741.         sysobjects o1, sysobjects o2,
  13742.         syscolumns c1, syscolumns c2,
  13743.         sysreferences r, sysconstraints s
  13744.     where    
  13745.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13746.     and    o1.name = @pk_table_name
  13747.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13748.     and    o1.id = r.rkeyid
  13749.     and s.id = o1.id and (s.status & 0xf) = 1
  13750.     and    o1.id = c1.id
  13751.     and    c1.colid = r.rkey2
  13752.     and    r.keycnt >= 2
  13753.     and r.fkeyid = o2.id
  13754.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13755.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13756.     and    o2.id = c2.id    
  13757.     and    c2.colid = r.fkey2
  13758.     union all
  13759.     select
  13760.         db_name(r.rkeydbid),
  13761.         user_name(o1.uid),
  13762.         o1.name,
  13763.         c1.name,
  13764.         convert(binary(16),null),
  13765.         convert(int,null),
  13766.         db_name(r.fkeydbid),
  13767.         user_name(o2.uid),
  13768.         o2.name,
  13769.         c2.name,
  13770.         convert(binary(16),null),
  13771.         convert(int,null),
  13772.         convert(int,3),
  13773.         'NO ACTION',
  13774.         'NO ACTION',
  13775.         object_name(s.constid),
  13776.         object_name(r.constid),
  13777.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13778.     from
  13779.         sysobjects o1, sysobjects o2,
  13780.         syscolumns c1, syscolumns c2,
  13781.         sysreferences r, sysconstraints s
  13782.     where    
  13783.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13784.     and    o1.name = @pk_table_name
  13785.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13786.     and    o1.id = r.rkeyid
  13787.     and s.id = o1.id and (s.status & 0xf) = 1
  13788.     and    o1.id = c1.id
  13789.     and    c1.colid = r.rkey3
  13790.     and    r.keycnt >= 3
  13791.     and r.fkeyid = o2.id
  13792.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13793.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13794.     and    o2.id = c2.id    
  13795.     and    c2.colid = r.fkey3
  13796.     union all
  13797.     select
  13798.         db_name(r.rkeydbid),
  13799.         user_name(o1.uid),
  13800.         o1.name,
  13801.         c1.name,
  13802.         convert(binary(16),null),
  13803.         convert(int,null),
  13804.         db_name(r.fkeydbid),
  13805.         user_name(o2.uid),
  13806.         o2.name,
  13807.         c2.name,
  13808.         convert(binary(16),null),
  13809.         convert(int,null),
  13810.         convert(int,4),
  13811.         'NO ACTION',
  13812.         'NO ACTION',
  13813.         object_name(s.constid),
  13814.         object_name(r.constid),
  13815.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13816.     from
  13817.         sysobjects o1, sysobjects o2,
  13818.         syscolumns c1, syscolumns c2,
  13819.         sysreferences r, sysconstraints s
  13820.     where    
  13821.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13822.     and    o1.name = @pk_table_name
  13823.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13824.     and    o1.id = r.rkeyid
  13825.     and s.id = o1.id and (s.status & 0xf) = 1
  13826.     and    r.keycnt >= 4
  13827.     and    o1.id = c1.id
  13828.     and    c1.colid = r.rkey4
  13829.     and r.fkeyid = o2.id
  13830.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13831.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13832.     and    o2.id = c2.id    
  13833.     and    c2.colid = r.fkey4
  13834.     union all
  13835.     select
  13836.         db_name(r.rkeydbid),
  13837.         user_name(o1.uid),
  13838.         o1.name,
  13839.         c1.name,
  13840.         convert(binary(16),null),
  13841.         convert(int,null),
  13842.         db_name(r.fkeydbid),
  13843.         user_name(o2.uid),
  13844.         o2.name,
  13845.         c2.name,
  13846.         convert(binary(16),null),
  13847.         convert(int,null),
  13848.         convert(int,5),
  13849.         'NO ACTION',
  13850.         'NO ACTION',
  13851.         object_name(s.constid),
  13852.         object_name(r.constid),
  13853.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13854.     from
  13855.         sysobjects o1, sysobjects o2,
  13856.         syscolumns c1, syscolumns c2,
  13857.         sysreferences r, sysconstraints s
  13858.     where    
  13859.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13860.     and    o1.name = @pk_table_name
  13861.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13862.     and    o1.id = r.rkeyid
  13863.     and s.id = o1.id and (s.status & 0xf) = 1
  13864.     and    r.keycnt >= 5
  13865.     and    o1.id = c1.id
  13866.     and    c1.colid = r.rkey5
  13867.     and r.fkeyid = o2.id
  13868.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13869.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13870.     and    o2.id = c2.id    
  13871.     and    c2.colid = r.fkey5
  13872.     union all
  13873.     select
  13874.         db_name(r.rkeydbid),
  13875.         user_name(o1.uid),
  13876.         o1.name,
  13877.         c1.name,
  13878.         convert(binary(16),null),
  13879.         convert(int,null),
  13880.         db_name(r.fkeydbid),
  13881.         user_name(o2.uid),
  13882.         o2.name,
  13883.         c2.name,
  13884.         convert(binary(16),null),
  13885.         convert(int,null),
  13886.         convert(int,6),
  13887.         'NO ACTION',
  13888.         'NO ACTION',
  13889.         object_name(s.constid),
  13890.         object_name(r.constid),
  13891.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13892.     from
  13893.         sysobjects o1, sysobjects o2,
  13894.         syscolumns c1, syscolumns c2,
  13895.         sysreferences r, sysconstraints s
  13896.     where    
  13897.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13898.     and    o1.name = @pk_table_name
  13899.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13900.     and    o1.id = r.rkeyid
  13901.     and s.id = o1.id and (s.status & 0xf) = 1
  13902.     and    r.keycnt >= 6
  13903.     and    o1.id = c1.id
  13904.     and    c1.colid = r.rkey6
  13905.     and r.fkeyid = o2.id
  13906.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13907.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13908.     and    o2.id = c2.id    
  13909.     and    c2.colid = r.fkey6
  13910.     union all
  13911.     select
  13912.         db_name(r.rkeydbid),
  13913.         user_name(o1.uid),
  13914.         o1.name,
  13915.         c1.name,
  13916.         convert(binary(16),null),
  13917.         convert(int,null),
  13918.         db_name(r.fkeydbid),
  13919.         user_name(o2.uid),
  13920.         o2.name,
  13921.         c2.name,
  13922.         convert(binary(16),null),
  13923.         convert(int,null),
  13924.         convert(int,7),
  13925.         'NO ACTION',
  13926.         'NO ACTION',
  13927.         object_name(s.constid),
  13928.         object_name(r.constid),
  13929.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13930.     from
  13931.         sysobjects o1, sysobjects o2,
  13932.         syscolumns c1, syscolumns c2,
  13933.         sysreferences r, sysconstraints s
  13934.     where    
  13935.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13936.     and    o1.name = @pk_table_name
  13937.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13938.     and    o1.id = r.rkeyid
  13939.     and s.id = o1.id and (s.status & 0xf) = 1
  13940.     and    r.keycnt >= 7
  13941.     and    o1.id = c1.id
  13942.     and    c1.colid = r.rkey7
  13943.     and r.fkeyid = o2.id
  13944.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13945.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13946.     and    o2.id = c2.id    
  13947.     and    c2.colid = r.fkey7
  13948.     union all
  13949.     select
  13950.         db_name(r.rkeydbid),
  13951.         user_name(o1.uid),
  13952.         o1.name,
  13953.         c1.name,
  13954.         convert(binary(16),null),
  13955.         convert(int,null),
  13956.         db_name(r.fkeydbid),
  13957.         user_name(o2.uid),
  13958.         o2.name,
  13959.         c2.name,
  13960.         convert(binary(16),null),
  13961.         convert(int,null),
  13962.         convert(int,8),
  13963.         'NO ACTION',
  13964.         'NO ACTION',
  13965.         object_name(s.constid),
  13966.         object_name(r.constid),
  13967.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13968.     from
  13969.         sysobjects o1, sysobjects o2,
  13970.         syscolumns c1, syscolumns c2,
  13971.         sysreferences r, sysconstraints s
  13972.     where    
  13973.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13974.     and    o1.name = @pk_table_name
  13975.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13976.     and    o1.id = r.rkeyid
  13977.     and s.id = o1.id and (s.status & 0xf) = 1
  13978.     and    r.keycnt >= 8
  13979.     and    o1.id = c1.id
  13980.     and    c1.colid = r.rkey8
  13981.     and r.fkeyid = o2.id
  13982.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13983.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13984.     and    o2.id = c2.id    
  13985.     and    c2.colid = r.fkey8
  13986.     union all
  13987.     select
  13988.         db_name(r.rkeydbid),
  13989.         user_name(o1.uid),
  13990.         o1.name,
  13991.         c1.name,
  13992.         convert(binary(16),null),
  13993.         convert(int,null),
  13994.         db_name(r.fkeydbid),
  13995.         user_name(o2.uid),
  13996.         o2.name,
  13997.         c2.name,
  13998.         convert(binary(16),null),
  13999.         convert(int,null),
  14000.         convert(int,9),
  14001.         'NO ACTION',
  14002.         'NO ACTION',
  14003.         object_name(s.constid),
  14004.         object_name(r.constid),
  14005.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14006.     from
  14007.         sysobjects o1, sysobjects o2,
  14008.         syscolumns c1, syscolumns c2,
  14009.         sysreferences r, sysconstraints s
  14010.     where    
  14011.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14012.     and    o1.name = @pk_table_name
  14013.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14014.     and    o1.id = r.rkeyid
  14015.     and s.id = o1.id and (s.status & 0xf) = 1
  14016.     and    r.keycnt >= 9
  14017.     and    o1.id = c1.id
  14018.     and    c1.colid = r.rkey9
  14019.     and r.fkeyid = o2.id
  14020.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14021.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14022.     and    o2.id = c2.id    
  14023.     and    c2.colid = r.fkey9
  14024.     union all
  14025.     select
  14026.         db_name(r.rkeydbid),
  14027.         user_name(o1.uid),
  14028.         o1.name,
  14029.         c1.name,
  14030.         convert(binary(16),null),
  14031.         convert(int,null),
  14032.         db_name(r.fkeydbid),
  14033.         user_name(o2.uid),
  14034.         o2.name,
  14035.         c2.name,
  14036.         convert(binary(16),null),
  14037.         convert(int,null),
  14038.         convert(int,10),
  14039.         'NO ACTION',
  14040.         'NO ACTION',
  14041.         object_name(s.constid),
  14042.         object_name(r.constid),
  14043.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14044.     from
  14045.         sysobjects o1, sysobjects o2,
  14046.         syscolumns c1, syscolumns c2,
  14047.         sysreferences r, sysconstraints s
  14048.     where    
  14049.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14050.     and    o1.name = @pk_table_name
  14051.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14052.     and    o1.id = r.rkeyid
  14053.     and s.id = o1.id and (s.status & 0xf) = 1
  14054.     and    r.keycnt >= 10
  14055.     and    o1.id = c1.id
  14056.     and    c1.colid = r.rkey10
  14057.     and r.fkeyid = o2.id
  14058.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14059.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14060.     and    o2.id = c2.id    
  14061.     and    c2.colid = r.fkey10
  14062.     union all
  14063.     select
  14064.         db_name(r.rkeydbid),
  14065.         user_name(o1.uid),
  14066.         o1.name,
  14067.         c1.name,
  14068.         convert(binary(16),null),
  14069.         convert(int,null),
  14070.         db_name(r.fkeydbid),
  14071.         user_name(o2.uid),
  14072.         o2.name,
  14073.         c2.name,
  14074.         convert(binary(16),null),
  14075.         convert(int,null),
  14076.         convert(int,11),
  14077.         'NO ACTION',
  14078.         'NO ACTION',
  14079.         object_name(s.constid),
  14080.         object_name(r.constid),
  14081.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14082.     from
  14083.         sysobjects o1, sysobjects o2,
  14084.         syscolumns c1, syscolumns c2,
  14085.         sysreferences r, sysconstraints s
  14086.     where    
  14087.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14088.     and    o1.name = @pk_table_name
  14089.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14090.     and    o1.id = r.rkeyid
  14091.     and s.id = o1.id and (s.status & 0xf) = 1
  14092.     and    r.keycnt >= 11
  14093.     and    o1.id = c1.id
  14094.     and    c1.colid = r.rkey11
  14095.     and r.fkeyid = o2.id
  14096.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14097.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14098.     and    o2.id = c2.id    
  14099.     and    c2.colid = r.fkey11
  14100.     union all
  14101.     select
  14102.         db_name(r.rkeydbid),
  14103.         user_name(o1.uid),
  14104.         o1.name,
  14105.         c1.name,
  14106.         convert(binary(16),null),
  14107.         convert(int,null),
  14108.         db_name(r.fkeydbid),
  14109.         user_name(o2.uid),
  14110.         o2.name,
  14111.         c2.name,
  14112.         convert(binary(16),null),
  14113.         convert(int,null),
  14114.         convert(int,12),
  14115.         'NO ACTION',
  14116.         'NO ACTION',
  14117.         object_name(s.constid),
  14118.         object_name(r.constid),
  14119.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14120.     from
  14121.         sysobjects o1, sysobjects o2,
  14122.         syscolumns c1, syscolumns c2,
  14123.         sysreferences r, sysconstraints s
  14124.     where    
  14125.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14126.     and    o1.name = @pk_table_name
  14127.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14128.     and    o1.id = r.rkeyid
  14129.     and s.id = o1.id and (s.status & 0xf) = 1
  14130.     and    r.keycnt >= 12
  14131.     and    o1.id = c1.id
  14132.     and    c1.colid = r.rkey12
  14133.     and r.fkeyid = o2.id
  14134.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14135.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14136.     and    o2.id = c2.id    
  14137.     and    c2.colid = r.fkey12
  14138.     union all
  14139.     select
  14140.         db_name(r.rkeydbid),
  14141.         user_name(o1.uid),
  14142.         o1.name,
  14143.         c1.name,
  14144.         convert(binary(16),null),
  14145.         convert(int,null),
  14146.         db_name(r.fkeydbid),
  14147.         user_name(o2.uid),
  14148.         o2.name,
  14149.         c2.name,
  14150.         convert(binary(16),null),
  14151.         convert(int,null),
  14152.         convert(int,13),
  14153.         'NO ACTION',
  14154.         'NO ACTION',
  14155.         object_name(s.constid),
  14156.         object_name(r.constid),
  14157.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14158.     from
  14159.         sysobjects o1, sysobjects o2,
  14160.         syscolumns c1, syscolumns c2,
  14161.         sysreferences r, sysconstraints s
  14162.     where    
  14163.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14164.     and    o1.name = @pk_table_name
  14165.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14166.     and    o1.id = r.rkeyid
  14167.     and s.id = o1.id and (s.status & 0xf) = 1
  14168.     and    r.keycnt >= 13
  14169.     and    o1.id = c1.id
  14170.     and    c1.colid = r.rkey13
  14171.     and r.fkeyid = o2.id
  14172.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14173.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14174.     and    o2.id = c2.id    
  14175.     and    c2.colid = r.fkey13
  14176.     union all
  14177.     select
  14178.         db_name(r.rkeydbid),
  14179.         user_name(o1.uid),
  14180.         o1.name,
  14181.         c1.name,
  14182.         convert(binary(16),null),
  14183.         convert(int,null),
  14184.         db_name(r.fkeydbid),
  14185.         user_name(o2.uid),
  14186.         o2.name,
  14187.         c2.name,
  14188.         convert(binary(16),null),
  14189.         convert(int,null),
  14190.         convert(int,14),
  14191.         'NO ACTION',
  14192.         'NO ACTION',
  14193.         object_name(s.constid),
  14194.         object_name(r.constid),
  14195.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14196.     from
  14197.         sysobjects o1, sysobjects o2,
  14198.         syscolumns c1, syscolumns c2,
  14199.         sysreferences r, sysconstraints s
  14200.     where    
  14201.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14202.     and    o1.name = @pk_table_name
  14203.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14204.     and    o1.id = r.rkeyid
  14205.     and s.id = o1.id and (s.status & 0xf) = 1
  14206.     and    r.keycnt >= 14
  14207.     and    o1.id = c1.id
  14208.     and    c1.colid = r.rkey14
  14209.     and r.fkeyid = o2.id
  14210.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14211.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14212.     and    o2.id = c2.id    
  14213.     and    c2.colid = r.fkey14
  14214.     union all
  14215.     select
  14216.         db_name(r.rkeydbid),
  14217.         user_name(o1.uid),
  14218.         o1.name,
  14219.         c1.name,
  14220.         convert(binary(16),null),
  14221.         convert(int,null),
  14222.         db_name(r.fkeydbid),
  14223.         user_name(o2.uid),
  14224.         o2.name,
  14225.         c2.name,
  14226.         convert(binary(16),null),
  14227.         convert(int,null),
  14228.         convert(int,15),
  14229.         'NO ACTION',
  14230.         'NO ACTION',
  14231.         object_name(s.constid),
  14232.         object_name(r.constid),
  14233.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14234.     from
  14235.         sysobjects o1, sysobjects o2,
  14236.         syscolumns c1, syscolumns c2,
  14237.         sysreferences r, sysconstraints s
  14238.     where    
  14239.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14240.     and    o1.name = @pk_table_name
  14241.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14242.     and    o1.id = r.rkeyid
  14243.     and s.id = o1.id and (s.status & 0xf) = 1
  14244.     and    r.keycnt >= 15
  14245.     and    o1.id = c1.id
  14246.     and    c1.colid = r.rkey15
  14247.     and r.fkeyid = o2.id
  14248.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14249.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14250.     and    o2.id = c2.id    
  14251.     and    c2.colid = r.fkey15
  14252.     union all
  14253.     select
  14254.         db_name(r.rkeydbid),
  14255.         user_name(o1.uid),
  14256.         o1.name,
  14257.         c1.name,
  14258.         convert(binary(16),null),
  14259.         convert(int,null),
  14260.         db_name(r.fkeydbid),
  14261.         user_name(o2.uid),
  14262.         o2.name,
  14263.         c2.name,
  14264.         convert(binary(16),null),
  14265.         convert(int,null),
  14266.         convert(int,16),
  14267.         'NO ACTION',
  14268.         'NO ACTION',
  14269.         object_name(s.constid),
  14270.         object_name(r.constid),
  14271.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14272.     from
  14273.         sysobjects o1, sysobjects o2,
  14274.         syscolumns c1, syscolumns c2,
  14275.         sysreferences r, sysconstraints s
  14276.     where    
  14277.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14278.     and    o1.name = @pk_table_name
  14279.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14280.     and    o1.id = r.rkeyid
  14281.     and s.id = o1.id and (s.status & 0xf) = 1
  14282.     and    r.keycnt >= 16
  14283.     and    o1.id = c1.id
  14284.     and    c1.colid = r.rkey16
  14285.     and r.fkeyid = o2.id
  14286.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14287.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14288.     and    o2.id = c2.id    
  14289.     and    c2.colid = r.fkey16
  14290.     order by 8,9,2,3,13
  14291.     END
  14292. ELSE IF @fk_table_name is not null
  14293.     BEGIN
  14294.     insert into #spfkeysrowset1
  14295.     select
  14296.         db_name(r.rkeydbid),
  14297.         user_name(o1.uid),
  14298.         o1.name,
  14299.         c1.name,
  14300.         convert(binary(16),null),
  14301.         convert(int,null),
  14302.         db_name(r.fkeydbid),
  14303.         user_name(o2.uid),
  14304.         o2.name,
  14305.         c2.name,
  14306.         convert(binary(16),null),
  14307.         convert(int,null),
  14308.         convert(int,1),
  14309.         'NO ACTION',
  14310.         'NO ACTION',
  14311.         object_name(s.constid),
  14312.         object_name(r.constid),
  14313.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14314.     from
  14315.         sysobjects o1, sysobjects o2,
  14316.         syscolumns c1, syscolumns c2,
  14317.         sysreferences r, sysconstraints s
  14318.     where    
  14319.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14320.     and    o2.name = @fk_table_name
  14321.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14322.     and    o2.id = r.fkeyid
  14323.     and    o2.id = c2.id
  14324.     and    c2.colid = r.fkey1
  14325.     and r.rkeyid = o1.id
  14326.     and s.id = o1.id and (s.status & 0xf) = 1
  14327.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14328.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14329.     and    o1.id = c1.id    
  14330.     and    c1.colid = r.rkey1
  14331.     union    all
  14332.     select
  14333.         db_name(r.rkeydbid),
  14334.         user_name(o1.uid),
  14335.         o1.name,
  14336.         c1.name,
  14337.         convert(binary(16),null),
  14338.         convert(int,null),
  14339.         db_name(r.fkeydbid),
  14340.         user_name(o2.uid),
  14341.         o2.name,
  14342.         c2.name,
  14343.         convert(binary(16),null),
  14344.         convert(int,null),
  14345.         convert(int,2),
  14346.         'NO ACTION',
  14347.         'NO ACTION',
  14348.         object_name(s.constid),
  14349.         object_name(r.constid),
  14350.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14351.     from
  14352.         sysobjects o1, sysobjects o2,
  14353.         syscolumns c1, syscolumns c2,
  14354.         sysreferences r, sysconstraints s
  14355.     where    
  14356.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14357.     and    o2.name = @fk_table_name
  14358.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14359.     and    o2.id = r.fkeyid
  14360.     and    r.keycnt >= 2
  14361.     and    o2.id = c2.id
  14362.     and    c2.colid = r.fkey2
  14363.     and r.rkeyid = o1.id
  14364.     and s.id = o1.id and (s.status & 0xf) = 1
  14365.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14366.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14367.     and    o1.id = c1.id    
  14368.     and    c1.colid = r.rkey2
  14369.     union    all
  14370.     select
  14371.         db_name(r.rkeydbid),
  14372.         user_name(o1.uid),
  14373.         o1.name,
  14374.         c1.name,
  14375.         convert(binary(16),null),
  14376.         convert(int,null),
  14377.         db_name(r.fkeydbid),
  14378.         user_name(o2.uid),
  14379.         o2.name,
  14380.         c2.name,
  14381.         convert(binary(16),null),
  14382.         convert(int,null),
  14383.         convert(int,3),
  14384.         'NO ACTION',
  14385.         'NO ACTION',
  14386.         object_name(s.constid),
  14387.         object_name(r.constid),
  14388.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14389.     from
  14390.         sysobjects o1, sysobjects o2,
  14391.         syscolumns c1, syscolumns c2,
  14392.         sysreferences r, sysconstraints s
  14393.     where    
  14394.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14395.     and    o2.name = @fk_table_name
  14396.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14397.     and    o2.id = r.fkeyid
  14398.     and    r.keycnt >= 3
  14399.     and    o2.id = c2.id
  14400.     and    c2.colid = r.fkey3
  14401.     and    r.rkeyid = o1.id
  14402.     and s.id = o1.id and (s.status & 0xf) = 1
  14403.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14404.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14405.     and    o1.id = c1.id    
  14406.     and    c1.colid = r.rkey3
  14407.     union    all
  14408.     select
  14409.         db_name(r.rkeydbid),
  14410.         user_name(o1.uid),
  14411.         o1.name,
  14412.         c1.name,
  14413.         convert(binary(16),null),
  14414.         convert(int,null),
  14415.         db_name(r.fkeydbid),
  14416.         user_name(o2.uid),
  14417.         o2.name,
  14418.         c2.name,
  14419.         convert(binary(16),null),
  14420.         convert(int,null),
  14421.         convert(int,4),
  14422.         'NO ACTION',
  14423.         'NO ACTION',
  14424.         object_name(s.constid),
  14425.         object_name(r.constid),
  14426.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14427.     from
  14428.         sysobjects o1, sysobjects o2,
  14429.         syscolumns c1, syscolumns c2,
  14430.         sysreferences r, sysconstraints s
  14431.     where    
  14432.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14433.     and    o2.name = @fk_table_name
  14434.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14435.     and    o2.id = r.fkeyid
  14436.     and    r.keycnt >= 4
  14437.     and    o2.id = c2.id
  14438.     and    c2.colid = r.fkey4
  14439.     and r.rkeyid = o1.id
  14440.     and s.id = o1.id and (s.status & 0xf) = 1
  14441.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14442.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14443.     and    o1.id = c1.id    
  14444.     and    c1.colid = r.rkey4
  14445.     union    all
  14446.     select
  14447.         db_name(r.rkeydbid),
  14448.         user_name(o1.uid),
  14449.         o1.name,
  14450.         c1.name,
  14451.         convert(binary(16),null),
  14452.         convert(int,null),
  14453.         db_name(r.fkeydbid),
  14454.         user_name(o2.uid),
  14455.         o2.name,
  14456.         c2.name,
  14457.         convert(binary(16),null),
  14458.         convert(int,null),
  14459.         convert(int,5),
  14460.         'NO ACTION',
  14461.         'NO ACTION',
  14462.         object_name(s.constid),
  14463.         object_name(r.constid),
  14464.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14465.     from
  14466.         sysobjects o1, sysobjects o2,
  14467.         syscolumns c1, syscolumns c2,
  14468.         sysreferences r, sysconstraints s
  14469.     where    
  14470.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14471.     and    o2.name = @fk_table_name
  14472.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14473.     and    o2.id = r.fkeyid
  14474.     and    r.keycnt >= 5
  14475.     and    o2.id = c2.id
  14476.     and    c2.colid = r.fkey5
  14477.     and r.rkeyid = o1.id
  14478.     and s.id = o1.id and (s.status & 0xf) = 1
  14479.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14480.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14481.     and    o1.id = c1.id    
  14482.     and    c1.colid = r.rkey5
  14483.     union    all
  14484.     select
  14485.         db_name(r.rkeydbid),
  14486.         user_name(o1.uid),
  14487.         o1.name,
  14488.         c1.name,
  14489.         convert(binary(16),null),
  14490.         convert(int,null),
  14491.         db_name(r.fkeydbid),
  14492.         user_name(o2.uid),
  14493.         o2.name,
  14494.         c2.name,
  14495.         convert(binary(16),null),
  14496.         convert(int,null),
  14497.         convert(int,6),
  14498.         'NO ACTION',
  14499.         'NO ACTION',
  14500.         object_name(s.constid),
  14501.         object_name(r.constid),
  14502.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14503.     from
  14504.         sysobjects o1, sysobjects o2,
  14505.         syscolumns c1, syscolumns c2,
  14506.         sysreferences r, sysconstraints s
  14507.     where    
  14508.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14509.     and    o2.name = @fk_table_name
  14510.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14511.     and    o2.id = r.fkeyid
  14512.     and    r.keycnt >= 6
  14513.     and    o2.id = c2.id
  14514.     and    c2.colid = r.fkey6
  14515.     and r.rkeyid = o1.id
  14516.     and s.id = o1.id and (s.status & 0xf) = 1
  14517.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14518.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14519.     and    o1.id = c1.id    
  14520.     and    c1.colid = r.rkey6
  14521.     union    all
  14522.     select
  14523.         db_name(r.rkeydbid),
  14524.         user_name(o1.uid),
  14525.         o1.name,
  14526.         c1.name,
  14527.         convert(binary(16),null),
  14528.         convert(int,null),
  14529.         db_name(r.fkeydbid),
  14530.         user_name(o2.uid),
  14531.         o2.name,
  14532.         c2.name,
  14533.         convert(binary(16),null),
  14534.         convert(int,null),
  14535.         convert(int,7),
  14536.         'NO ACTION',
  14537.         'NO ACTION',
  14538.         object_name(s.constid),
  14539.         object_name(r.constid),
  14540.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14541.     from
  14542.         sysobjects o1, sysobjects o2,
  14543.         syscolumns c1, syscolumns c2,
  14544.         sysreferences r, sysconstraints s
  14545.     where    
  14546.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14547.     and    o2.name = @fk_table_name
  14548.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14549.     and    o2.id = r.fkeyid
  14550.     and    r.keycnt >= 7
  14551.     and    o2.id = c2.id
  14552.     and    c2.colid = r.fkey7
  14553.     and r.rkeyid = o1.id
  14554.     and s.id = o1.id and (s.status & 0xf) = 1
  14555.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14556.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14557.     and    o1.id = c1.id    
  14558.     and    c1.colid = r.rkey7
  14559.     union    all
  14560.     select
  14561.         db_name(r.rkeydbid),
  14562.         user_name(o1.uid),
  14563.         o1.name,
  14564.         c1.name,
  14565.         convert(binary(16),null),
  14566.         convert(int,null),
  14567.         db_name(r.fkeydbid),
  14568.         user_name(o2.uid),
  14569.         o2.name,
  14570.         c2.name,
  14571.         convert(binary(16),null),
  14572.         convert(int,null),
  14573.         convert(int,8),
  14574.         'NO ACTION',
  14575.         'NO ACTION',
  14576.         object_name(s.constid),
  14577.         object_name(r.constid),
  14578.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14579.     from
  14580.         sysobjects o1, sysobjects o2,
  14581.         syscolumns c1, syscolumns c2,
  14582.         sysreferences r, sysconstraints s
  14583.     where    
  14584.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14585.     and    o2.name = @fk_table_name
  14586.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14587.     and    o2.id = r.fkeyid
  14588.     and    r.keycnt >= 8
  14589.     and    o2.id = c2.id
  14590.     and    c2.colid = r.fkey8
  14591.     and r.rkeyid = o1.id
  14592.     and s.id = o1.id and (s.status & 0xf) = 1
  14593.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14594.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14595.     and    o1.id = c1.id    
  14596.     and    c1.colid = r.rkey8
  14597.     union    all
  14598.     select
  14599.         db_name(r.rkeydbid),
  14600.         user_name(o1.uid),
  14601.         o1.name,
  14602.         c1.name,
  14603.         convert(binary(16),null),
  14604.         convert(int,null),
  14605.         db_name(r.fkeydbid),
  14606.         user_name(o2.uid),
  14607.         o2.name,
  14608.         c2.name,
  14609.         convert(binary(16),null),
  14610.         convert(int,null),
  14611.         convert(int,9),
  14612.         'NO ACTION',
  14613.         'NO ACTION',
  14614.         object_name(s.constid),
  14615.         object_name(r.constid),
  14616.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14617.     from
  14618.         sysobjects o1, sysobjects o2,
  14619.         syscolumns c1, syscolumns c2,
  14620.         sysreferences r, sysconstraints s
  14621.     where    
  14622.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14623.     and    o2.name = @fk_table_name
  14624.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14625.     and    o2.id = r.fkeyid
  14626.     and    r.keycnt >= 9
  14627.     and    o2.id = c2.id
  14628.     and    c2.colid = r.fkey9
  14629.     and r.rkeyid = o1.id
  14630.     and s.id = o1.id and (s.status & 0xf) = 1
  14631.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14632.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14633.     and    o1.id = c1.id    
  14634.     and    c1.colid = r.rkey9
  14635.     union    all
  14636.     select
  14637.         db_name(r.rkeydbid),
  14638.         user_name(o1.uid),
  14639.         o1.name,
  14640.         c1.name,
  14641.         convert(binary(16),null),
  14642.         convert(int,null),
  14643.         db_name(r.fkeydbid),
  14644.         user_name(o2.uid),
  14645.         o2.name,
  14646.         c2.name,
  14647.         convert(binary(16),null),
  14648.         convert(int,null),
  14649.         convert(int,10),
  14650.         'NO ACTION',
  14651.         'NO ACTION',
  14652.         object_name(s.constid),
  14653.         object_name(r.constid),
  14654.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14655.     from
  14656.         sysobjects o1, sysobjects o2,
  14657.         syscolumns c1, syscolumns c2,
  14658.         sysreferences r, sysconstraints s
  14659.     where    
  14660.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14661.     and    o2.name = @fk_table_name
  14662.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14663.     and    o2.id = r.fkeyid
  14664.     and    r.keycnt >= 10
  14665.     and    o2.id = c2.id
  14666.     and    c2.colid = r.fkey10
  14667.     and r.rkeyid = o1.id
  14668.     and s.id = o1.id and (s.status & 0xf) = 1
  14669.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14670.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14671.     and    o1.id = c1.id    
  14672.     and    c1.colid = r.rkey10
  14673.     union    all
  14674.     select
  14675.         db_name(r.rkeydbid),
  14676.         user_name(o1.uid),
  14677.         o1.name,
  14678.         c1.name,
  14679.         convert(binary(16),null),
  14680.         convert(int,null),
  14681.         db_name(r.fkeydbid),
  14682.         user_name(o2.uid),
  14683.         o2.name,
  14684.         c2.name,
  14685.         convert(binary(16),null),
  14686.         convert(int,null),
  14687.         convert(int,11),
  14688.         'NO ACTION',
  14689.         'NO ACTION',
  14690.         object_name(s.constid),
  14691.         object_name(r.constid),
  14692.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14693.     from
  14694.         sysobjects o1, sysobjects o2,
  14695.         syscolumns c1, syscolumns c2,
  14696.         sysreferences r, sysconstraints s
  14697.     where    
  14698.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14699.     and    o2.name = @fk_table_name
  14700.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14701.     and    o2.id = r.fkeyid
  14702.     and    r.keycnt >= 11
  14703.     and    o2.id = c2.id
  14704.     and    c2.colid = r.fkey11
  14705.     and r.rkeyid = o1.id
  14706.     and s.id = o1.id and (s.status & 0xf) = 1
  14707.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14708.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14709.     and    o1.id = c1.id    
  14710.     and    c1.colid = r.rkey11
  14711.     union    all
  14712.     select
  14713.         db_name(r.rkeydbid),
  14714.         user_name(o1.uid),
  14715.         o1.name,
  14716.         c1.name,
  14717.         convert(binary(16),null),
  14718.         convert(int,null),
  14719.         db_name(r.fkeydbid),
  14720.         user_name(o2.uid),
  14721.         o2.name,
  14722.         c2.name,
  14723.         convert(binary(16),null),
  14724.         convert(int,null),
  14725.         convert(int,12),
  14726.         'NO ACTION',
  14727.         'NO ACTION',
  14728.         object_name(s.constid),
  14729.         object_name(r.constid),
  14730.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  14731.     from
  14732.         sysobjects o1, sysobjects o2,
  14733.         syscolumns c1, syscolumns c2,
  14734.         sysreferences r, sysconstraints s
  14735.     where    
  14736.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14737.     and    o2.name = @fk_table_name
  14738.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14739.     and    o2.id = r.fkeyid
  14740.     and    r.keycnt >= 12
  14741.     and    o2.id = c2.id
  14742.     and    c2.colid = r.fkey12
  14743.     and r.rkeyid = o1.id
  14744.     and s.id = o1.id and (s.status & 0xf) = 1
  14745.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14746.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14747.     and    o1.id = c1.id    
  14748.     and    c1.colid = r.rkey12
  14749.     union    all
  14750.     select
  14751.         db_name(r.rkeydbid),
  14752.         user_name(o1.uid),
  14753.         o1.name,
  14754.         c1.name,
  14755.         convert(binary(16),null),
  14756.         convert(int,null),
  14757.         db_name(r.fkeydbid),
  14758.         user_name(o2.uid),
  14759.         o2.name,
  14760.         c2.name,
  14761.         convert(binary(16),null),
  14762.         convert(int,null),
  14763.         convert(int,13),
  14764.         'NO ACTION',
  14765.         'NO ACTION',
  14766.         object_name(s.constid),
  14767.         object_name(r.constid),
  14768.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14769.     from
  14770.         sysobjects o1, sysobjects o2,
  14771.         syscolumns c1, syscolumns c2,
  14772.         sysreferences r, sysconstraints s
  14773.     where    
  14774.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14775.     and    o2.name = @fk_table_name
  14776.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14777.     and    o2.id = r.fkeyid
  14778.     and    r.keycnt >= 13
  14779.     and    o2.id = c2.id
  14780.     and    c2.colid = r.fkey13
  14781.     and r.rkeyid = o1.id
  14782.     and s.id = o1.id and (s.status & 0xf) = 1
  14783.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14784.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14785.     and    o1.id = c1.id    
  14786.     and    c1.colid = r.rkey13
  14787.     union    all
  14788.     select
  14789.         db_name(r.rkeydbid),
  14790.         user_name(o1.uid),
  14791.         o1.name,
  14792.         c1.name,
  14793.         convert(binary(16),null),
  14794.         convert(int,null),
  14795.         db_name(r.fkeydbid),
  14796.         user_name(o2.uid),
  14797.         o2.name,
  14798.         c2.name,
  14799.         convert(binary(16),null),
  14800.         convert(int,null),
  14801.         convert(int,14),
  14802.         'NO ACTION',
  14803.         'NO ACTION',
  14804.         object_name(s.constid),
  14805.         object_name(r.constid),
  14806.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14807.     from
  14808.         sysobjects o1, sysobjects o2,
  14809.         syscolumns c1, syscolumns c2,
  14810.         sysreferences r, sysconstraints s
  14811.     where    
  14812.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14813.     and    o2.name = @fk_table_name
  14814.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14815.     and    o2.id = r.fkeyid
  14816.     and    r.keycnt >= 14
  14817.     and    o2.id = c2.id
  14818.     and    c2.colid = r.fkey14
  14819.     and r.rkeyid = o1.id
  14820.     and s.id = o1.id and (s.status & 0xf) = 1
  14821.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14822.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14823.     and    o1.id = c1.id    
  14824.     and    c1.colid = r.rkey14
  14825.     union    all
  14826.     select
  14827.         db_name(r.rkeydbid),
  14828.         user_name(o1.uid),
  14829.         o1.name,
  14830.         c1.name,
  14831.         convert(binary(16),null),
  14832.         convert(int,null),
  14833.         db_name(r.fkeydbid),
  14834.         user_name(o2.uid),
  14835.         o2.name,
  14836.         c2.name,
  14837.         convert(binary(16),null),
  14838.         convert(int,null),
  14839.         convert(int,15),
  14840.         'NO ACTION',
  14841.         'NO ACTION',
  14842.         object_name(s.constid),
  14843.         object_name(r.constid),
  14844.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14845.     from
  14846.         sysobjects o1, sysobjects o2,
  14847.         syscolumns c1, syscolumns c2,
  14848.         sysreferences r, sysconstraints s
  14849.     where    
  14850.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14851.     and    o2.name = @fk_table_name
  14852.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14853.     and    o2.id = r.fkeyid
  14854.     and    r.keycnt >= 15
  14855.     and    o2.id = c2.id
  14856.     and    c2.colid = r.fkey15
  14857.     and r.rkeyid = o1.id
  14858.     and s.id = o1.id and (s.status & 0xf) = 1
  14859.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14860.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14861.     and    o1.id = c1.id    
  14862.     and    c1.colid = r.rkey15
  14863.     union    all
  14864.     select
  14865.         db_name(r.rkeydbid),
  14866.         user_name(o1.uid),
  14867.         o1.name,
  14868.         c1.name,
  14869.         convert(binary(16),null),
  14870.         convert(int,null),
  14871.         db_name(r.fkeydbid),
  14872.         user_name(o2.uid),
  14873.         o2.name,
  14874.         c2.name,
  14875.         convert(binary(16),null),
  14876.         convert(int,null),
  14877.         convert(int,16),
  14878.         'NO ACTION',
  14879.         'NO ACTION',
  14880.         object_name(s.constid),
  14881.         object_name(r.constid),
  14882.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14883.     from
  14884.         sysobjects o1, sysobjects o2,
  14885.         syscolumns c1, syscolumns c2,
  14886.         sysreferences r, sysconstraints s
  14887.     where    
  14888.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14889.     and    o2.name = @fk_table_name
  14890.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14891.     and    o2.id = r.fkeyid
  14892.     and    r.keycnt >= 16
  14893.     and    o2.id = c2.id
  14894.     and    c2.colid = r.fkey16
  14895.     and r.rkeyid = o1.id
  14896.     and s.id = o1.id and (s.status & 0xf) = 1
  14897.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14898.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14899.     and    o1.id = c1.id    
  14900.     and    c1.colid = r.rkey16
  14901.     order by 8,9,2,3,13
  14902.     END
  14903. ELSE
  14904.     BEGIN
  14905.     insert into #spfkeysrowset1
  14906.     select
  14907.         db_name(r.rkeydbid),
  14908.         user_name(o1.uid),
  14909.         o1.name,
  14910.         c1.name,
  14911.         convert(binary(16),null),
  14912.         convert(int,null),
  14913.         db_name(r.fkeydbid),
  14914.         user_name(o2.uid),
  14915.         o2.name,
  14916.         c2.name,
  14917.         convert(binary(16),null),
  14918.         convert(int,null),
  14919.         convert(int,1),
  14920.         'NO ACTION',
  14921.         'NO ACTION',
  14922.         object_name(s.constid),
  14923.         object_name(r.constid),
  14924.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14925.     from
  14926.         sysobjects o1, sysobjects o2,
  14927.         syscolumns c1, syscolumns c2,
  14928.         sysreferences r, sysconstraints s
  14929.     where    
  14930.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14931.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14932.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14933.     and    o1.id = r.rkeyid
  14934.     and s.id = o1.id and (s.status & 0xf) = 1
  14935.     and    o1.id = c1.id
  14936.     and    c1.colid = r.rkey1
  14937.     and r.fkeyid = o2.id
  14938.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14939.     and    o2.id = c2.id    
  14940.     and    c2.colid = r.fkey1
  14941.     union all
  14942.     select
  14943.         db_name(r.rkeydbid),
  14944.         user_name(o1.uid),
  14945.         o1.name,
  14946.         c1.name,
  14947.         convert(binary(16),null),
  14948.         convert(int,null),
  14949.         db_name(r.fkeydbid),
  14950.         user_name(o2.uid),
  14951.         o2.name,
  14952.         c2.name,
  14953.         convert(binary(16),null),
  14954.         convert(int,null),
  14955.         convert(int,2),
  14956.         'NO ACTION',
  14957.         'NO ACTION',
  14958.         object_name(s.constid),
  14959.         object_name(r.constid),
  14960.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14961.     from
  14962.         sysobjects o1, sysobjects o2,
  14963.         syscolumns c1, syscolumns c2,
  14964.         sysreferences r, sysconstraints s
  14965.     where    
  14966.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14967.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14968.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14969.     and    o1.id = r.rkeyid
  14970.     and s.id = o1.id and (s.status & 0xf) = 1
  14971.     and    r.keycnt >= 2
  14972.     and    o1.id = c1.id
  14973.     and    c1.colid = r.rkey2
  14974.     and r.fkeyid = o2.id
  14975.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14976.     and    o2.id = c2.id    
  14977.     and    c2.colid = r.fkey2
  14978.     union all
  14979.     select
  14980.         db_name(r.rkeydbid),
  14981.         user_name(o1.uid),
  14982.         o1.name,
  14983.         c1.name,
  14984.         convert(binary(16),null),
  14985.         convert(int,null),
  14986.         db_name(r.fkeydbid),
  14987.         user_name(o2.uid),
  14988.         o2.name,
  14989.         c2.name,
  14990.         convert(binary(16),null),
  14991.         convert(int,null),
  14992.         convert(int,3),
  14993.         'NO ACTION',
  14994.         'NO ACTION',
  14995.         object_name(s.constid),
  14996.         object_name(r.constid),
  14997.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14998.     from
  14999.         sysobjects o1, sysobjects o2,
  15000.         syscolumns c1, syscolumns c2,
  15001.         sysreferences r, sysconstraints s
  15002.     where    
  15003.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15004.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15005.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15006.     and    o1.id = r.rkeyid
  15007.     and s.id = o1.id and (s.status & 0xf) = 1
  15008.     and    r.keycnt >= 3
  15009.     and    o1.id = c1.id
  15010.     and    c1.colid = r.rkey3
  15011.     and r.fkeyid = o2.id
  15012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15013.     and    o2.id = c2.id    
  15014.     and    c2.colid = r.fkey3
  15015.     union all
  15016.     select
  15017.         db_name(r.rkeydbid),
  15018.         user_name(o1.uid),
  15019.         o1.name,
  15020.         c1.name,
  15021.         convert(binary(16),null),
  15022.         convert(int,null),
  15023.         db_name(r.fkeydbid),
  15024.         user_name(o2.uid),
  15025.         o2.name,
  15026.         c2.name,
  15027.         convert(binary(16),null),
  15028.         convert(int,null),
  15029.         convert(int,4),
  15030.         'NO ACTION',
  15031.         'NO ACTION',
  15032.         object_name(s.constid),
  15033.         object_name(r.constid),
  15034.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15035.     from
  15036.         sysobjects o1, sysobjects o2,
  15037.         syscolumns c1, syscolumns c2,
  15038.         sysreferences r, sysconstraints s
  15039.     where    
  15040.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15041.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15042.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15043.     and    o1.id = r.rkeyid
  15044.     and s.id = o1.id and (s.status & 0xf) = 1
  15045.     and    r.keycnt >= 4
  15046.     and    o1.id = c1.id
  15047.     and    c1.colid = r.rkey4
  15048.     and r.fkeyid = o2.id
  15049.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15050.     and    o2.id = c2.id    
  15051.     and    c2.colid = r.fkey4
  15052.     union all
  15053.     select
  15054.         db_name(r.rkeydbid),
  15055.         user_name(o1.uid),
  15056.         o1.name,
  15057.         c1.name,
  15058.         convert(binary(16),null),
  15059.         convert(int,null),
  15060.         db_name(r.fkeydbid),
  15061.         user_name(o2.uid),
  15062.         o2.name,
  15063.         c2.name,
  15064.         convert(binary(16),null),
  15065.         convert(int,null),
  15066.         convert(int,5),
  15067.         'NO ACTION',
  15068.         'NO ACTION',
  15069.         object_name(s.constid),
  15070.         object_name(r.constid),
  15071.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15072.     from
  15073.         sysobjects o1, sysobjects o2,
  15074.         syscolumns c1, syscolumns c2,
  15075.         sysreferences r, sysconstraints s
  15076.     where    
  15077.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15078.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15079.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15080.     and    o1.id = r.rkeyid
  15081.     and s.id = o1.id and (s.status & 0xf) = 1
  15082.     and    r.keycnt >= 5
  15083.     and    o1.id = c1.id
  15084.     and    c1.colid = r.rkey5
  15085.     and r.fkeyid = o2.id
  15086.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15087.     and    o2.id = c2.id    
  15088.     and    c2.colid = r.fkey5
  15089.     union all
  15090.     select
  15091.         db_name(r.rkeydbid),
  15092.         user_name(o1.uid),
  15093.         o1.name,
  15094.         c1.name,
  15095.         convert(binary(16),null),
  15096.         convert(int,null),
  15097.         db_name(r.fkeydbid),
  15098.         user_name(o2.uid),
  15099.         o2.name,
  15100.         c2.name,
  15101.         convert(binary(16),null),
  15102.         convert(int,null),
  15103.         convert(int,6),
  15104.         'NO ACTION',
  15105.         'NO ACTION',
  15106.         object_name(s.constid),
  15107.         object_name(r.constid),
  15108.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15109.     from
  15110.         sysobjects o1, sysobjects o2,
  15111.         syscolumns c1, syscolumns c2,
  15112.         sysreferences r, sysconstraints s
  15113.     where    
  15114.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15115.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15116.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15117.     and    o1.id = r.rkeyid
  15118.     and s.id = o1.id and (s.status & 0xf) = 1
  15119.     and    r.keycnt >= 6
  15120.     and    o1.id = c1.id
  15121.     and    c1.colid = r.rkey6
  15122.     and r.fkeyid = o2.id
  15123.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15124.     and    o2.id = c2.id    
  15125.     and    c2.colid = r.fkey6
  15126.     union all
  15127.     select
  15128.         db_name(r.rkeydbid),
  15129.         user_name(o1.uid),
  15130.         o1.name,
  15131.         c1.name,
  15132.         convert(binary(16),null),
  15133.         convert(int,null),
  15134.         db_name(r.fkeydbid),
  15135.         user_name(o2.uid),
  15136.         o2.name,
  15137.         c2.name,
  15138.         convert(binary(16),null),
  15139.         convert(int,null),
  15140.         convert(int,7),
  15141.         'NO ACTION',
  15142.         'NO ACTION',
  15143.         object_name(s.constid),
  15144.         object_name(r.constid),
  15145.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15146.     from
  15147.         sysobjects o1, sysobjects o2,
  15148.         syscolumns c1, syscolumns c2,
  15149.         sysreferences r, sysconstraints s
  15150.     where    
  15151.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15152.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15153.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15154.     and    o1.id = r.rkeyid
  15155.     and s.id = o1.id and (s.status & 0xf) = 1
  15156.     and    r.keycnt >= 7
  15157.     and    o1.id = c1.id
  15158.     and    c1.colid = r.rkey7
  15159.     and r.fkeyid = o2.id
  15160.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15161.     and    o2.id = c2.id    
  15162.     and    c2.colid = r.fkey7
  15163.     union all
  15164.     select
  15165.         db_name(r.rkeydbid),
  15166.         user_name(o1.uid),
  15167.         o1.name,
  15168.         c1.name,
  15169.         convert(binary(16),null),
  15170.         convert(int,null),
  15171.         db_name(r.fkeydbid),
  15172.         user_name(o2.uid),
  15173.         o2.name,
  15174.         c2.name,
  15175.         convert(binary(16),null),
  15176.         convert(int,null),
  15177.         convert(int,8),
  15178.         'NO ACTION',
  15179.         'NO ACTION',
  15180.         object_name(s.constid),
  15181.         object_name(r.constid),
  15182.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15183.     from
  15184.         sysobjects o1, sysobjects o2,
  15185.         syscolumns c1, syscolumns c2,
  15186.         sysreferences r, sysconstraints s
  15187.     where    
  15188.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15189.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15190.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15191.     and    o1.id = r.rkeyid
  15192.     and s.id = o1.id and (s.status & 0xf) = 1
  15193.     and    r.keycnt >= 8
  15194.     and    o1.id = c1.id
  15195.     and    c1.colid = r.rkey8
  15196.     and r.fkeyid = o2.id
  15197.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15198.     and    o2.id = c2.id    
  15199.     and    c2.colid = r.fkey8
  15200.     union all
  15201.     select
  15202.         db_name(r.rkeydbid),
  15203.         user_name(o1.uid),
  15204.         o1.name,
  15205.         c1.name,
  15206.         convert(binary(16),null),
  15207.         convert(int,null),
  15208.         db_name(r.fkeydbid),
  15209.         user_name(o2.uid),
  15210.         o2.name,
  15211.         c2.name,
  15212.         convert(binary(16),null),
  15213.         convert(int,null),
  15214.         convert(int,9),
  15215.         'NO ACTION',
  15216.         'NO ACTION',
  15217.         object_name(s.constid),
  15218.         object_name(r.constid),
  15219.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15220.     from
  15221.         sysobjects o1, sysobjects o2,
  15222.         syscolumns c1, syscolumns c2,
  15223.         sysreferences r, sysconstraints s
  15224.     where    
  15225.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15226.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15227.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15228.     and    o1.id = r.rkeyid
  15229.     and s.id = o1.id and (s.status & 0xf) = 1
  15230.     and    r.keycnt >= 9
  15231.     and    o1.id = c1.id
  15232.     and    c1.colid = r.rkey9
  15233.     and r.fkeyid = o2.id
  15234.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15235.     and    o2.id = c2.id    
  15236.     and    c2.colid = r.fkey9
  15237.     union all
  15238.     select
  15239.         db_name(r.rkeydbid),
  15240.         user_name(o1.uid),
  15241.         o1.name,
  15242.         c1.name,
  15243.         convert(binary(16),null),
  15244.         convert(int,null),
  15245.         db_name(r.fkeydbid),
  15246.         user_name(o2.uid),
  15247.         o2.name,
  15248.         c2.name,
  15249.         convert(binary(16),null),
  15250.         convert(int,null),
  15251.         convert(int,10),
  15252.         'NO ACTION',
  15253.         'NO ACTION',
  15254.         object_name(s.constid),
  15255.         object_name(r.constid),
  15256.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15257.     from
  15258.         sysobjects o1, sysobjects o2,
  15259.         syscolumns c1, syscolumns c2,
  15260.         sysreferences r, sysconstraints s
  15261.     where    
  15262.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15263.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15264.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15265.     and    o1.id = r.rkeyid
  15266.     and s.id = o1.id and (s.status & 0xf) = 1
  15267.     and    r.keycnt >= 10
  15268.     and    o1.id = c1.id
  15269.     and    c1.colid = r.rkey10
  15270.     and r.fkeyid = o2.id
  15271.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15272.     and    o2.id = c2.id    
  15273.     and    c2.colid = r.fkey10
  15274.     union all
  15275.     select
  15276.         db_name(r.rkeydbid),
  15277.         user_name(o1.uid),
  15278.         o1.name,
  15279.         c1.name,
  15280.         convert(binary(16),null),
  15281.         convert(int,null),
  15282.         db_name(r.fkeydbid),
  15283.         user_name(o2.uid),
  15284.         o2.name,
  15285.         c2.name,
  15286.         convert(binary(16),null),
  15287.         convert(int,null),
  15288.         convert(int,11),
  15289.         'NO ACTION',
  15290.         'NO ACTION',
  15291.         object_name(s.constid),
  15292.         object_name(r.constid),
  15293.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15294.     from
  15295.         sysobjects o1, sysobjects o2,
  15296.         syscolumns c1, syscolumns c2,
  15297.         sysreferences r, sysconstraints s
  15298.     where    
  15299.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15300.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15301.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15302.     and    o1.id = r.rkeyid
  15303.     and s.id = o1.id and (s.status & 0xf) = 1
  15304.     and    r.keycnt >= 11
  15305.     and    o1.id = c1.id
  15306.     and    c1.colid = r.rkey11
  15307.     and r.fkeyid = o2.id
  15308.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15309.     and    o2.id = c2.id    
  15310.     and    c2.colid = r.fkey11
  15311.     union all
  15312.     select
  15313.         db_name(r.rkeydbid),
  15314.         user_name(o1.uid),
  15315.         o1.name,
  15316.         c1.name,
  15317.         convert(binary(16),null),
  15318.         convert(int,null),
  15319.         db_name(r.fkeydbid),
  15320.         user_name(o2.uid),
  15321.         o2.name,
  15322.         c2.name,
  15323.         convert(binary(16),null),
  15324.         convert(int,null),
  15325.         convert(int,12),
  15326.         'NO ACTION',
  15327.         'NO ACTION',
  15328.         object_name(s.constid),
  15329.         object_name(r.constid),
  15330.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15331.     from
  15332.         sysobjects o1, sysobjects o2,
  15333.         syscolumns c1, syscolumns c2,
  15334.         sysreferences r, sysconstraints s
  15335.     where    
  15336.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15337.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15338.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15339.     and    o1.id = r.rkeyid
  15340.     and s.id = o1.id and (s.status & 0xf) = 1
  15341.     and    r.keycnt >= 12
  15342.     and    o1.id = c1.id
  15343.     and    c1.colid = r.rkey12
  15344.     and r.fkeyid = o2.id
  15345.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15346.     and    o2.id = c2.id    
  15347.     and    c2.colid = r.fkey12
  15348.     union all
  15349.     select
  15350.         db_name(r.rkeydbid),
  15351.         user_name(o1.uid),
  15352.         o1.name,
  15353.         c1.name,
  15354.         convert(binary(16),null),
  15355.         convert(int,null),
  15356.         db_name(r.fkeydbid),
  15357.         user_name(o2.uid),
  15358.         o2.name,
  15359.         c2.name,
  15360.         convert(binary(16),null),
  15361.         convert(int,null),
  15362.         convert(int,13),
  15363.         'NO ACTION',
  15364.         'NO ACTION',
  15365.         object_name(s.constid),
  15366.         object_name(r.constid),
  15367.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15368.     from
  15369.         sysobjects o1, sysobjects o2,
  15370.         syscolumns c1, syscolumns c2,
  15371.         sysreferences r, sysconstraints s
  15372.     where    
  15373.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15374.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15375.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15376.     and    o1.id = r.rkeyid
  15377.     and s.id = o1.id and (s.status & 0xf) = 1
  15378.     and    r.keycnt >= 13
  15379.     and    o1.id = c1.id
  15380.     and    c1.colid = r.rkey13
  15381.     and r.fkeyid = o2.id
  15382.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15383.     and    o2.id = c2.id    
  15384.     and    c2.colid = r.fkey13
  15385.     union all
  15386.     select
  15387.         db_name(r.rkeydbid),
  15388.         user_name(o1.uid),
  15389.         o1.name,
  15390.         c1.name,
  15391.         convert(binary(16),null),
  15392.         convert(int,null),
  15393.         db_name(r.fkeydbid),
  15394.         user_name(o2.uid),
  15395.         o2.name,
  15396.         c2.name,
  15397.         convert(binary(16),null),
  15398.         convert(int,null),
  15399.         convert(int,14),
  15400.         'NO ACTION',
  15401.         'NO ACTION',
  15402.         object_name(s.constid),
  15403.         object_name(r.constid),
  15404.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15405.     from
  15406.         sysobjects o1, sysobjects o2,
  15407.         syscolumns c1, syscolumns c2,
  15408.         sysreferences r, sysconstraints s
  15409.     where    
  15410.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15411.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15412.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15413.     and    o1.id = r.rkeyid
  15414.     and s.id = o1.id and (s.status & 0xf) = 1
  15415.     and    r.keycnt >= 14
  15416.     and    o1.id = c1.id
  15417.     and    c1.colid = r.rkey14
  15418.     and r.fkeyid = o2.id
  15419.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15420.     and    o2.id = c2.id    
  15421.     and    c2.colid = r.fkey14
  15422.     union all
  15423.     select
  15424.         db_name(r.rkeydbid),
  15425.         user_name(o1.uid),
  15426.         o1.name,
  15427.         c1.name,
  15428.         convert(binary(16),null),
  15429.         convert(int,null),
  15430.         db_name(r.fkeydbid),
  15431.         user_name(o2.uid),
  15432.         o2.name,
  15433.         c2.name,
  15434.         convert(binary(16),null),
  15435.         convert(int,null),
  15436.         convert(int,15),
  15437.         'NO ACTION',
  15438.         'NO ACTION',
  15439.         object_name(s.constid),
  15440.         object_name(r.constid),
  15441.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15442.     from
  15443.         sysobjects o1, sysobjects o2,
  15444.         syscolumns c1, syscolumns c2,
  15445.         sysreferences r, sysconstraints s
  15446.     where    
  15447.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15448.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15449.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15450.     and    o1.id = r.rkeyid
  15451.     and s.id = o1.id and (s.status & 0xf) = 1
  15452.     and    r.keycnt >= 15
  15453.     and    o1.id = c1.id
  15454.     and    c1.colid = r.rkey15
  15455.     and r.fkeyid = o2.id
  15456.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15457.     and    o2.id = c2.id    
  15458.     and    c2.colid = r.fkey15
  15459.     union all
  15460.     select
  15461.         db_name(r.rkeydbid),
  15462.         user_name(o1.uid),
  15463.         o1.name,
  15464.         c1.name,
  15465.         convert(binary(16),null),
  15466.         convert(int,null),
  15467.         db_name(r.fkeydbid),
  15468.         user_name(o2.uid),
  15469.         o2.name,
  15470.         c2.name,
  15471.         convert(binary(16),null),
  15472.         convert(int,null),
  15473.         convert(int,16),
  15474.         'NO ACTION',
  15475.         'NO ACTION',
  15476.         object_name(s.constid),
  15477.         object_name(r.constid),
  15478.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15479.     from
  15480.         sysobjects o1, sysobjects o2,
  15481.         syscolumns c1, syscolumns c2,
  15482.         sysreferences r, sysconstraints s
  15483.     where    
  15484.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15485.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15486.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15487.     and    o1.id = r.rkeyid
  15488.     and s.id = o1.id and (s.status & 0xf) = 1
  15489.     and    o1.id = c1.id
  15490.     and    r.keycnt >= 16
  15491.     and    c1.colid = r.rkey16
  15492.     and r.fkeyid = o2.id
  15493.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15494.     and    o2.id = c2.id    
  15495.     and    c2.colid = r.fkey16
  15496.     order by 8,9,2,3,13
  15497.     END
  15498.  
  15499.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  15500.         @scrollopt output, @ccopt output, @rows output
  15501.  
  15502.     drop table #spfkeysrowset1
  15503.        return isnull(@ret,0)
  15504. go
  15505. dump tran master with no_log
  15506. go
  15507. CREATE PROCEDURE sp_foreign_keys_rowset;3
  15508. as
  15509.     select
  15510.         PK_TABLE_CATALOG    = convert(sysname,' '),
  15511.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  15512.         PK_TABLE_NAME         = convert(sysname,' '),
  15513.         PK_COLUMN_NAME         = convert(sysname,' '),
  15514.         PK_COLUMN_GUID        = convert(binary(16),null),
  15515.         PK_COLUMN_PROPID    = convert(int,null),
  15516.         FK_TABLE_CATALOG    = convert(sysname,' '),
  15517.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  15518.         FK_TABLE_NAME         = convert(sysname,' '),
  15519.         FK_COLUMN_NAME         = convert(sysname,' '),
  15520.         FK_COLUMN_GUID        = convert(binary(16),null),
  15521.         FK_COLUMN_PROPID    = convert(int,null),
  15522.         ORDINAL                = convert(int,1),
  15523.         UPDATE_RULE            = 'NO ACTION',
  15524.         DELETE_RULE         = 'NO ACTION',
  15525.         PK_NAME                = convert(sysname, ' '),
  15526.         FK_NAME                = convert(sysname, ' '),
  15527.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15528.     where    1=0
  15529. go
  15530. dump tran master with no_log
  15531. go
  15532.  
  15533.  
  15534. if (charindex('7.00', @@version) = 0)
  15535. begin
  15536.     print ''
  15537.     print ''
  15538.     print 'Warning:'
  15539.     print 'you are installing the stored procedures '
  15540.     print 'on a pre 7.0 SQL Server.'
  15541.     print 'Ignore the following errors.'
  15542. end
  15543. else
  15544.     drop proc sp_foreign_keys_rowset
  15545. go
  15546.  
  15547.  
  15548. /*    Procedure for 7.0 server */
  15549. CREATE PROCEDURE sp_foreign_keys_rowset
  15550.     (
  15551.     @pk_table_name        sysname,
  15552.     @pk_table_schema    sysname = null,
  15553.     @fk_table_name        sysname = null,
  15554.     @fk_table_schema    sysname = null,
  15555.     @fk_table_catalog    sysname = null
  15556.     )
  15557. as
  15558.     select
  15559.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15560.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15561.         PK_TABLE_NAME         = o1.name,
  15562.         PK_COLUMN_NAME         = c1.name,
  15563.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15564.         PK_COLUMN_PROPID    = convert(int,null),
  15565.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15566.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15567.         FK_TABLE_NAME         = o2.name,
  15568.         FK_COLUMN_NAME         = c2.name,
  15569.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15570.         FK_COLUMN_PROPID    = convert(int,null),
  15571.         ORDINAL                = convert(int,1),
  15572.         UPDATE_RULE            = N'NO ACTION',
  15573.         DELETE_RULE         = N'NO ACTION',
  15574.         PK_NAME                = object_name(s.constid),
  15575.         FK_NAME                = object_name(r.constid),
  15576.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15577.     from
  15578.         sysobjects o1, sysobjects o2,
  15579.         syscolumns c1, syscolumns c2,
  15580.         sysreferences r, sysconstraints s
  15581.     where    
  15582.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15583.     and    o1.name = @pk_table_name
  15584.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15585.     and    o1.id = r.rkeyid
  15586.     and s.id = o1.id and (s.status & 0xf) = 1
  15587.     and    o1.id = c1.id
  15588.     and    c1.colid = r.rkey1
  15589.     and r.fkeyid = o2.id
  15590.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15591.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15592.     and    o2.id = c2.id    
  15593.     and    c2.colid = r.fkey1
  15594.     union all
  15595.     select
  15596.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15597.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15598.         PK_TABLE_NAME         = o1.name,
  15599.         PK_COLUMN_NAME         = c1.name,
  15600.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15601.         PK_COLUMN_PROPID    = convert(int,null),
  15602.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15603.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15604.         FK_TABLE_NAME         = o2.name,
  15605.         FK_COLUMN_NAME         = c2.name,
  15606.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15607.         FK_COLUMN_PROPID    = convert(int,null),
  15608.         ORDINAL                = convert(int,2),
  15609.         UPDATE_RULE            = N'NO ACTION',
  15610.         DELETE_RULE         = N'NO ACTION',
  15611.         PK_NAME                = object_name(s.constid),
  15612.         FK_NAME                = object_name(r.constid),
  15613.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15614.     from
  15615.         sysobjects o1, sysobjects o2,
  15616.         syscolumns c1, syscolumns c2,
  15617.         sysreferences r, sysconstraints s
  15618.     where    
  15619.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15620.     and    o1.name = @pk_table_name
  15621.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15622.     and    o1.id = r.rkeyid
  15623.     and s.id = o1.id and (s.status & 0xf) = 1
  15624.     and    o1.id = c1.id
  15625.     and    c1.colid = r.rkey2
  15626.     and    r.keycnt >= 2
  15627.     and r.fkeyid = o2.id
  15628.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15629.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15630.     and    o2.id = c2.id    
  15631.     and    c2.colid = r.fkey2
  15632.     union all
  15633.     select
  15634.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15635.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15636.         PK_TABLE_NAME         = o1.name,
  15637.         PK_COLUMN_NAME         = c1.name,
  15638.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15639.         PK_COLUMN_PROPID    = convert(int,null),
  15640.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15641.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15642.         FK_TABLE_NAME         = o2.name,
  15643.         FK_COLUMN_NAME         = c2.name,
  15644.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15645.         FK_COLUMN_PROPID    = convert(int,null),
  15646.         ORDINAL                = convert(int,3),
  15647.         UPDATE_RULE            = N'NO ACTION',
  15648.         DELETE_RULE         = N'NO ACTION',
  15649.         PK_NAME                = object_name(s.constid),
  15650.         FK_NAME                = object_name(r.constid),
  15651.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15652.     from
  15653.         sysobjects o1, sysobjects o2,
  15654.         syscolumns c1, syscolumns c2,
  15655.         sysreferences r, sysconstraints s
  15656.     where    
  15657.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15658.     and    o1.name = @pk_table_name
  15659.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15660.     and    o1.id = r.rkeyid
  15661.     and s.id = o1.id and (s.status & 0xf) = 1
  15662.     and    o1.id = c1.id
  15663.     and    c1.colid = r.rkey3
  15664.     and    r.keycnt >= 3
  15665.     and r.fkeyid = o2.id
  15666.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15667.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15668.     and    o2.id = c2.id    
  15669.     and    c2.colid = r.fkey3
  15670.     union all
  15671.     select
  15672.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15673.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15674.         PK_TABLE_NAME         = o1.name,
  15675.         PK_COLUMN_NAME         = c1.name,
  15676.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15677.         PK_COLUMN_PROPID    = convert(int,null),
  15678.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15679.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15680.         FK_TABLE_NAME         = o2.name,
  15681.         FK_COLUMN_NAME         = c2.name,
  15682.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15683.         FK_COLUMN_PROPID    = convert(int,null),
  15684.         ORDINAL                = convert(int,4),
  15685.         UPDATE_RULE            = N'NO ACTION',
  15686.         DELETE_RULE         = N'NO ACTION',
  15687.         PK_NAME                = object_name(s.constid),
  15688.         FK_NAME                = object_name(r.constid),
  15689.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15690.     from
  15691.         sysobjects o1, sysobjects o2,
  15692.         syscolumns c1, syscolumns c2,
  15693.         sysreferences r, sysconstraints s
  15694.     where    
  15695.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15696.     and    o1.name = @pk_table_name
  15697.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15698.     and    o1.id = r.rkeyid
  15699.     and s.id = o1.id and (s.status & 0xf) = 1
  15700.     and    r.keycnt >= 4
  15701.     and    o1.id = c1.id
  15702.     and    c1.colid = r.rkey4
  15703.     and r.fkeyid = o2.id
  15704.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15705.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15706.     and    o2.id = c2.id    
  15707.     and    c2.colid = r.fkey4
  15708.     union all
  15709.     select
  15710.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15711.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15712.         PK_TABLE_NAME         = o1.name,
  15713.         PK_COLUMN_NAME         = c1.name,
  15714.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15715.         PK_COLUMN_PROPID    = convert(int,null),
  15716.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15717.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15718.         FK_TABLE_NAME         = o2.name,
  15719.         FK_COLUMN_NAME         = c2.name,
  15720.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15721.         FK_COLUMN_PROPID    = convert(int,null),
  15722.         ORDINAL                = convert(int,5),
  15723.         UPDATE_RULE            = N'NO ACTION',
  15724.         DELETE_RULE         = N'NO ACTION',
  15725.         PK_NAME                = object_name(s.constid),
  15726.         FK_NAME                = object_name(r.constid),
  15727.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15728.     from
  15729.         sysobjects o1, sysobjects o2,
  15730.         syscolumns c1, syscolumns c2,
  15731.         sysreferences r, sysconstraints s
  15732.     where    
  15733.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15734.     and    o1.name = @pk_table_name
  15735.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15736.     and    o1.id = r.rkeyid
  15737.     and s.id = o1.id and (s.status & 0xf) = 1
  15738.     and    r.keycnt >= 5
  15739.     and    o1.id = c1.id
  15740.     and    c1.colid = r.rkey5
  15741.     and r.fkeyid = o2.id
  15742.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15743.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15744.     and    o2.id = c2.id    
  15745.     and    c2.colid = r.fkey5
  15746.     union all
  15747.     select
  15748.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15749.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15750.         PK_TABLE_NAME         = o1.name,
  15751.         PK_COLUMN_NAME         = c1.name,
  15752.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15753.         PK_COLUMN_PROPID    = convert(int,null),
  15754.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15755.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15756.         FK_TABLE_NAME         = o2.name,
  15757.         FK_COLUMN_NAME         = c2.name,
  15758.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15759.         FK_COLUMN_PROPID    = convert(int,null),
  15760.         ORDINAL                = convert(int,6),
  15761.         UPDATE_RULE            = N'NO ACTION',
  15762.         DELETE_RULE         = N'NO ACTION',
  15763.         PK_NAME                = object_name(s.constid),
  15764.         FK_NAME                = object_name(r.constid),
  15765.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15766.     from
  15767.         sysobjects o1, sysobjects o2,
  15768.         syscolumns c1, syscolumns c2,
  15769.         sysreferences r, sysconstraints s
  15770.     where    
  15771.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15772.     and    o1.name = @pk_table_name
  15773.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15774.     and    o1.id = r.rkeyid
  15775.     and s.id = o1.id and (s.status & 0xf) = 1
  15776.     and    r.keycnt >= 6
  15777.     and    o1.id = c1.id
  15778.     and    c1.colid = r.rkey6
  15779.     and r.fkeyid = o2.id
  15780.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15781.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15782.     and    o2.id = c2.id    
  15783.     and    c2.colid = r.fkey6
  15784.     union all
  15785.     select
  15786.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15787.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15788.         PK_TABLE_NAME         = o1.name,
  15789.         PK_COLUMN_NAME         = c1.name,
  15790.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15791.         PK_COLUMN_PROPID    = convert(int,null),
  15792.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15793.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15794.         FK_TABLE_NAME         = o2.name,
  15795.         FK_COLUMN_NAME         = c2.name,
  15796.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15797.         FK_COLUMN_PROPID    = convert(int,null),
  15798.         ORDINAL                = convert(int,7),
  15799.         UPDATE_RULE            = N'NO ACTION',
  15800.         DELETE_RULE         = N'NO ACTION',
  15801.         PK_NAME                = object_name(s.constid),
  15802.         FK_NAME                = object_name(r.constid),
  15803.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15804.     from
  15805.         sysobjects o1, sysobjects o2,
  15806.         syscolumns c1, syscolumns c2,
  15807.         sysreferences r, sysconstraints s
  15808.     where    
  15809.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15810.     and    o1.name = @pk_table_name
  15811.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15812.     and    o1.id = r.rkeyid
  15813.     and s.id = o1.id and (s.status & 0xf) = 1
  15814.     and    r.keycnt >= 7
  15815.     and    o1.id = c1.id
  15816.     and    c1.colid = r.rkey7
  15817.     and r.fkeyid = o2.id
  15818.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15819.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15820.     and    o2.id = c2.id    
  15821.     and    c2.colid = r.fkey7
  15822.     union all
  15823.     select
  15824.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15825.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15826.         PK_TABLE_NAME         = o1.name,
  15827.         PK_COLUMN_NAME         = c1.name,
  15828.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15829.         PK_COLUMN_PROPID    = convert(int,null),
  15830.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15831.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15832.         FK_TABLE_NAME         = o2.name,
  15833.         FK_COLUMN_NAME         = c2.name,
  15834.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15835.         FK_COLUMN_PROPID    = convert(int,null),
  15836.         ORDINAL                = convert(int,8),
  15837.         UPDATE_RULE            = N'NO ACTION',
  15838.         DELETE_RULE         = N'NO ACTION',
  15839.         PK_NAME                = object_name(s.constid),
  15840.         FK_NAME                = object_name(r.constid),
  15841.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15842.     from
  15843.         sysobjects o1, sysobjects o2,
  15844.         syscolumns c1, syscolumns c2,
  15845.         sysreferences r, sysconstraints s
  15846.     where    
  15847.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15848.     and    o1.name = @pk_table_name
  15849.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15850.     and    o1.id = r.rkeyid
  15851.     and s.id = o1.id and (s.status & 0xf) = 1
  15852.     and    r.keycnt >= 8
  15853.     and    o1.id = c1.id
  15854.     and    c1.colid = r.rkey8
  15855.     and r.fkeyid = o2.id
  15856.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15857.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15858.     and    o2.id = c2.id    
  15859.     and    c2.colid = r.fkey8
  15860.     union all
  15861.     select
  15862.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15863.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15864.         PK_TABLE_NAME         = o1.name,
  15865.         PK_COLUMN_NAME         = c1.name,
  15866.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15867.         PK_COLUMN_PROPID    = convert(int,null),
  15868.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15869.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15870.         FK_TABLE_NAME         = o2.name,
  15871.         FK_COLUMN_NAME         = c2.name,
  15872.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15873.         FK_COLUMN_PROPID    = convert(int,null),
  15874.         ORDINAL                = convert(int,9),
  15875.         UPDATE_RULE            = N'NO ACTION',
  15876.         DELETE_RULE         = N'NO ACTION',
  15877.         PK_NAME                = object_name(s.constid),
  15878.         FK_NAME                = object_name(r.constid),
  15879.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15880.     from
  15881.         sysobjects o1, sysobjects o2,
  15882.         syscolumns c1, syscolumns c2,
  15883.         sysreferences r, sysconstraints s
  15884.     where    
  15885.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15886.     and    o1.name = @pk_table_name
  15887.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15888.     and    o1.id = r.rkeyid
  15889.     and s.id = o1.id and (s.status & 0xf) = 1
  15890.     and    r.keycnt >= 9
  15891.     and    o1.id = c1.id
  15892.     and    c1.colid = r.rkey9
  15893.     and r.fkeyid = o2.id
  15894.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15895.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15896.     and    o2.id = c2.id    
  15897.     and    c2.colid = r.fkey9
  15898.     union all
  15899.     select
  15900.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15901.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15902.         PK_TABLE_NAME         = o1.name,
  15903.         PK_COLUMN_NAME         = c1.name,
  15904.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15905.         PK_COLUMN_PROPID    = convert(int,null),
  15906.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15907.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15908.         FK_TABLE_NAME         = o2.name,
  15909.         FK_COLUMN_NAME         = c2.name,
  15910.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15911.         FK_COLUMN_PROPID    = convert(int,null),
  15912.         ORDINAL                = convert(int,10),
  15913.         UPDATE_RULE            = N'NO ACTION',
  15914.         DELETE_RULE         = N'NO ACTION',
  15915.         PK_NAME                = object_name(s.constid),
  15916.         FK_NAME                = object_name(r.constid),
  15917.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15918.     from
  15919.         sysobjects o1, sysobjects o2,
  15920.         syscolumns c1, syscolumns c2,
  15921.         sysreferences r, sysconstraints s
  15922.     where    
  15923.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15924.     and    o1.name = @pk_table_name
  15925.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15926.     and    o1.id = r.rkeyid
  15927.     and s.id = o1.id and (s.status & 0xf) = 1
  15928.     and    r.keycnt >= 10
  15929.     and    o1.id = c1.id
  15930.     and    c1.colid = r.rkey10
  15931.     and r.fkeyid = o2.id
  15932.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15933.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15934.     and    o2.id = c2.id    
  15935.     and    c2.colid = r.fkey10
  15936.     union all
  15937.     select
  15938.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15939.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15940.         PK_TABLE_NAME         = o1.name,
  15941.         PK_COLUMN_NAME         = c1.name,
  15942.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15943.         PK_COLUMN_PROPID    = convert(int,null),
  15944.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15945.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15946.         FK_TABLE_NAME         = o2.name,
  15947.         FK_COLUMN_NAME         = c2.name,
  15948.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15949.         FK_COLUMN_PROPID    = convert(int,null),
  15950.         ORDINAL                = convert(int,11),
  15951.         UPDATE_RULE            = N'NO ACTION',
  15952.         DELETE_RULE         = N'NO ACTION',
  15953.         PK_NAME                = object_name(s.constid),
  15954.         FK_NAME                = object_name(r.constid),
  15955.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15956.     from
  15957.         sysobjects o1, sysobjects o2,
  15958.         syscolumns c1, syscolumns c2,
  15959.         sysreferences r, sysconstraints s
  15960.     where    
  15961.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15962.     and    o1.name = @pk_table_name
  15963.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15964.     and    o1.id = r.rkeyid
  15965.     and s.id = o1.id and (s.status & 0xf) = 1
  15966.     and    r.keycnt >= 11
  15967.     and    o1.id = c1.id
  15968.     and    c1.colid = r.rkey11
  15969.     and r.fkeyid = o2.id
  15970.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15971.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15972.     and    o2.id = c2.id    
  15973.     and    c2.colid = r.fkey11
  15974.     union all
  15975.     select
  15976.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15977.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15978.         PK_TABLE_NAME         = o1.name,
  15979.         PK_COLUMN_NAME         = c1.name,
  15980.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15981.         PK_COLUMN_PROPID    = convert(int,null),
  15982.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15983.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15984.         FK_TABLE_NAME         = o2.name,
  15985.         FK_COLUMN_NAME         = c2.name,
  15986.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15987.         FK_COLUMN_PROPID    = convert(int,null),
  15988.         ORDINAL                = convert(int,12),
  15989.         UPDATE_RULE            = N'NO ACTION',
  15990.         DELETE_RULE         = N'NO ACTION',
  15991.         PK_NAME                = object_name(s.constid),
  15992.         FK_NAME                = object_name(r.constid),
  15993.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15994.     from
  15995.         sysobjects o1, sysobjects o2,
  15996.         syscolumns c1, syscolumns c2,
  15997.         sysreferences r, sysconstraints s
  15998.     where    
  15999.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16000.     and    o1.name = @pk_table_name
  16001.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16002.     and    o1.id = r.rkeyid
  16003.     and s.id = o1.id and (s.status & 0xf) = 1
  16004.     and    r.keycnt >= 12
  16005.     and    o1.id = c1.id
  16006.     and    c1.colid = r.rkey12
  16007.     and r.fkeyid = o2.id
  16008.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16009.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16010.     and    o2.id = c2.id    
  16011.     and    c2.colid = r.fkey12
  16012.     union all
  16013.     select
  16014.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16015.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16016.         PK_TABLE_NAME         = o1.name,
  16017.         PK_COLUMN_NAME         = c1.name,
  16018.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16019.         PK_COLUMN_PROPID    = convert(int,null),
  16020.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16021.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16022.         FK_TABLE_NAME         = o2.name,
  16023.         FK_COLUMN_NAME         = c2.name,
  16024.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16025.         FK_COLUMN_PROPID    = convert(int,null),
  16026.         ORDINAL                = convert(int,13),
  16027.         UPDATE_RULE            = N'NO ACTION',
  16028.         DELETE_RULE         = N'NO ACTION',
  16029.         PK_NAME                = object_name(s.constid),
  16030.         FK_NAME                = object_name(r.constid),
  16031.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16032.     from
  16033.         sysobjects o1, sysobjects o2,
  16034.         syscolumns c1, syscolumns c2,
  16035.         sysreferences r, sysconstraints s
  16036.     where    
  16037.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16038.     and    o1.name = @pk_table_name
  16039.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16040.     and    o1.id = r.rkeyid
  16041.     and s.id = o1.id and (s.status & 0xf) = 1
  16042.     and    r.keycnt >= 13
  16043.     and    o1.id = c1.id
  16044.     and    c1.colid = r.rkey13
  16045.     and r.fkeyid = o2.id
  16046.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16047.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16048.     and    o2.id = c2.id    
  16049.     and    c2.colid = r.fkey13
  16050.     union all
  16051.     select
  16052.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16053.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16054.         PK_TABLE_NAME         = o1.name,
  16055.         PK_COLUMN_NAME         = c1.name,
  16056.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16057.         PK_COLUMN_PROPID    = convert(int,null),
  16058.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16059.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16060.         FK_TABLE_NAME         = o2.name,
  16061.         FK_COLUMN_NAME         = c2.name,
  16062.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16063.         FK_COLUMN_PROPID    = convert(int,null),
  16064.         ORDINAL                = convert(int,14),
  16065.         UPDATE_RULE            = N'NO ACTION',
  16066.         DELETE_RULE         = N'NO ACTION',
  16067.         PK_NAME                = object_name(s.constid),
  16068.         FK_NAME                = object_name(r.constid),
  16069.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16070.     from
  16071.         sysobjects o1, sysobjects o2,
  16072.         syscolumns c1, syscolumns c2,
  16073.         sysreferences r, sysconstraints s
  16074.     where    
  16075.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16076.     and    o1.name = @pk_table_name
  16077.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16078.     and    o1.id = r.rkeyid
  16079.     and s.id = o1.id and (s.status & 0xf) = 1
  16080.     and    r.keycnt >= 14
  16081.     and    o1.id = c1.id
  16082.     and    c1.colid = r.rkey14
  16083.     and r.fkeyid = o2.id
  16084.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16085.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16086.     and    o2.id = c2.id    
  16087.     and    c2.colid = r.fkey14
  16088.     union all
  16089.     select
  16090.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16091.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16092.         PK_TABLE_NAME         = o1.name,
  16093.         PK_COLUMN_NAME         = c1.name,
  16094.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16095.         PK_COLUMN_PROPID    = convert(int,null),
  16096.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16097.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16098.         FK_TABLE_NAME         = o2.name,
  16099.         FK_COLUMN_NAME         = c2.name,
  16100.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16101.         FK_COLUMN_PROPID    = convert(int,null),
  16102.         ORDINAL                = convert(int,15),
  16103.         UPDATE_RULE            = N'NO ACTION',
  16104.         DELETE_RULE         = N'NO ACTION',
  16105.         PK_NAME                = object_name(s.constid),
  16106.         FK_NAME                = object_name(r.constid),
  16107.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16108.     from
  16109.         sysobjects o1, sysobjects o2,
  16110.         syscolumns c1, syscolumns c2,
  16111.         sysreferences r, sysconstraints s
  16112.     where    
  16113.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16114.     and    o1.name = @pk_table_name
  16115.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16116.     and    o1.id = r.rkeyid
  16117.     and s.id = o1.id and (s.status & 0xf) = 1
  16118.     and    r.keycnt >= 15
  16119.     and    o1.id = c1.id
  16120.     and    c1.colid = r.rkey15
  16121.     and r.fkeyid = o2.id
  16122.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16123.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16124.     and    o2.id = c2.id    
  16125.     and    c2.colid = r.fkey15
  16126.     union all
  16127.     select
  16128.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16129.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16130.         PK_TABLE_NAME         = o1.name,
  16131.         PK_COLUMN_NAME         = c1.name,
  16132.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16133.         PK_COLUMN_PROPID    = convert(int,null),
  16134.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16135.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16136.         FK_TABLE_NAME         = o2.name,
  16137.         FK_COLUMN_NAME         = c2.name,
  16138.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16139.         FK_COLUMN_PROPID    = convert(int,null),
  16140.         ORDINAL                = convert(int,16),
  16141.         UPDATE_RULE            = N'NO ACTION',
  16142.         DELETE_RULE         = N'NO ACTION',
  16143.         PK_NAME                = object_name(s.constid),
  16144.         FK_NAME                = object_name(r.constid),
  16145.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16146.     from
  16147.         sysobjects o1, sysobjects o2,
  16148.         syscolumns c1, syscolumns c2,
  16149.         sysreferences r, sysconstraints s
  16150.     where    
  16151.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16152.     and    o1.name = @pk_table_name
  16153.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16154.     and    o1.id = r.rkeyid
  16155.     and s.id = o1.id and (s.status & 0xf) = 1
  16156.     and    r.keycnt >= 16
  16157.     and    o1.id = c1.id
  16158.     and    c1.colid = r.rkey16
  16159.     and r.fkeyid = o2.id
  16160.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16161.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16162.     and    o2.id = c2.id    
  16163.     and    c2.colid = r.fkey16
  16164.     order by 8,9,2,3,13
  16165. go
  16166. dump tran master with no_log
  16167. go
  16168. CREATE PROCEDURE sp_foreign_keys_rowset;2
  16169.     (
  16170.     @fk_table_name        sysname,
  16171.     @fk_table_schema    sysname = null,
  16172.     @pk_table_name        sysname = null,
  16173.     @pk_table_schema    sysname = null,
  16174.     @pk_table_catalog    sysname = null
  16175.     )
  16176. as
  16177.     select
  16178.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16179.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16180.         PK_TABLE_NAME         = o1.name,
  16181.         PK_COLUMN_NAME         = c1.name,
  16182.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16183.         PK_COLUMN_PROPID    = convert(int,null),
  16184.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16185.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16186.         FK_TABLE_NAME         = o2.name,
  16187.         FK_COLUMN_NAME         = c2.name,
  16188.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16189.         FK_COLUMN_PROPID    = convert(int,null),
  16190.         ORDINAL                = convert(int,1),
  16191.         UPDATE_RULE            = N'NO ACTION',
  16192.         DELETE_RULE         = N'NO ACTION',
  16193.         PK_NAME                = object_name(s.constid),
  16194.         FK_NAME                = object_name(r.constid),
  16195.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16196.     from
  16197.         sysobjects o1, sysobjects o2,
  16198.         syscolumns c1, syscolumns c2,
  16199.         sysreferences r, sysconstraints s
  16200.     where    
  16201.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16202.     and    o2.name = @fk_table_name
  16203.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16204.     and    o2.id = r.fkeyid
  16205.     and    o2.id = c2.id
  16206.     and    c2.colid = r.fkey1
  16207.     and r.rkeyid = o1.id
  16208.     and s.id = o1.id and (s.status & 0xf) = 1
  16209.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16210.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16211.     and    o1.id = c1.id    
  16212.     and    c1.colid = r.rkey1
  16213.     union    all
  16214.     select
  16215.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16216.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16217.         PK_TABLE_NAME         = o1.name,
  16218.         PK_COLUMN_NAME         = c1.name,
  16219.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16220.         PK_COLUMN_PROPID    = convert(int,null),
  16221.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16222.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16223.         FK_TABLE_NAME         = o2.name,
  16224.         FK_COLUMN_NAME         = c2.name,
  16225.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16226.         FK_COLUMN_PROPID    = convert(int,null),
  16227.         ORDINAL                = convert(int,2),
  16228.         UPDATE_RULE            = N'NO ACTION',
  16229.         DELETE_RULE         = N'NO ACTION',
  16230.         PK_NAME                = object_name(s.constid),
  16231.         FK_NAME                = object_name(r.constid),
  16232.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16233.     from
  16234.         sysobjects o1, sysobjects o2,
  16235.         syscolumns c1, syscolumns c2,
  16236.         sysreferences r, sysconstraints s
  16237.     where    
  16238.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16239.     and    o2.name = @fk_table_name
  16240.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16241.     and    o2.id = r.fkeyid
  16242.     and    r.keycnt >= 2
  16243.     and    o2.id = c2.id
  16244.     and    c2.colid = r.fkey2
  16245.     and    r.rkeyid = o1.id
  16246.     and s.id = o1.id and (s.status & 0xf) = 1
  16247.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16248.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16249.     and    o1.id = c1.id    
  16250.     and    c1.colid = r.rkey2
  16251.     union    all
  16252.     select
  16253.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16254.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16255.         PK_TABLE_NAME         = o1.name,
  16256.         PK_COLUMN_NAME         = c1.name,
  16257.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16258.         PK_COLUMN_PROPID    = convert(int,null),
  16259.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16260.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16261.         FK_TABLE_NAME         = o2.name,
  16262.         FK_COLUMN_NAME         = c2.name,
  16263.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16264.         FK_COLUMN_PROPID    = convert(int,null),
  16265.         ORDINAL                = convert(int,3),
  16266.         UPDATE_RULE            = N'NO ACTION',
  16267.         DELETE_RULE         = N'NO ACTION',
  16268.         PK_NAME                = object_name(s.constid),
  16269.         FK_NAME                = object_name(r.constid),
  16270.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16271.     from
  16272.         sysobjects o1, sysobjects o2,
  16273.         syscolumns c1, syscolumns c2,
  16274.         sysreferences r, sysconstraints s
  16275.     where    
  16276.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16277.     and    o2.name = @fk_table_name
  16278.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16279.     and    o2.id = r.fkeyid
  16280.     and    r.keycnt >= 3
  16281.     and    o2.id = c2.id
  16282.     and    c2.colid = r.fkey3
  16283.     and r.rkeyid = o1.id
  16284.     and s.id = o1.id and (s.status & 0xf) = 1
  16285.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16286.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16287.     and    o1.id = c1.id    
  16288.     and    c1.colid = r.rkey3
  16289.     union    all
  16290.     select
  16291.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16292.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16293.         PK_TABLE_NAME         = o1.name,
  16294.         PK_COLUMN_NAME         = c1.name,
  16295.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16296.         PK_COLUMN_PROPID    = convert(int,null),
  16297.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16298.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16299.         FK_TABLE_NAME         = o2.name,
  16300.         FK_COLUMN_NAME         = c2.name,
  16301.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16302.         FK_COLUMN_PROPID    = convert(int,null),
  16303.         ORDINAL                = convert(int,4),
  16304.         UPDATE_RULE            = N'NO ACTION',
  16305.         DELETE_RULE         = N'NO ACTION',
  16306.         PK_NAME                = object_name(s.constid),
  16307.         FK_NAME                = object_name(r.constid),
  16308.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16309.     from
  16310.         sysobjects o1, sysobjects o2,
  16311.         syscolumns c1, syscolumns c2,
  16312.         sysreferences r, sysconstraints s
  16313.     where    
  16314.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16315.     and    o2.name = @fk_table_name
  16316.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16317.     and    o2.id = r.fkeyid
  16318.     and    r.keycnt >= 4
  16319.     and    o2.id = c2.id
  16320.     and    c2.colid = r.fkey4
  16321.     and r.rkeyid = o1.id
  16322.     and s.id = o1.id and (s.status & 0xf) = 1
  16323.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16324.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16325.     and    o1.id = c1.id    
  16326.     and    c1.colid = r.rkey4
  16327.     union    all
  16328.     select
  16329.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16330.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16331.         PK_TABLE_NAME         = o1.name,
  16332.         PK_COLUMN_NAME         = c1.name,
  16333.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16334.         PK_COLUMN_PROPID    = convert(int,null),
  16335.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16336.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16337.         FK_TABLE_NAME         = o2.name,
  16338.         FK_COLUMN_NAME         = c2.name,
  16339.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16340.         FK_COLUMN_PROPID    = convert(int,null),
  16341.         ORDINAL                = convert(int,5),
  16342.         UPDATE_RULE            = N'NO ACTION',
  16343.         DELETE_RULE         = N'NO ACTION',
  16344.         PK_NAME                = object_name(s.constid),
  16345.         FK_NAME                = object_name(r.constid),
  16346.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16347.     from
  16348.         sysobjects o1, sysobjects o2,
  16349.         syscolumns c1, syscolumns c2,
  16350.         sysreferences r, sysconstraints s
  16351.     where    
  16352.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16353.     and    o2.name = @fk_table_name
  16354.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16355.     and    o2.id = r.fkeyid
  16356.     and    r.keycnt >= 5
  16357.     and    o2.id = c2.id
  16358.     and    c2.colid = r.fkey5
  16359.     and    r.rkeyid = o1.id
  16360.     and s.id = o1.id and (s.status & 0xf) = 1
  16361.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16362.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16363.     and    o1.id = c1.id    
  16364.     and    c1.colid = r.rkey5
  16365.     union    all
  16366.     select
  16367.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16368.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16369.         PK_TABLE_NAME         = o1.name,
  16370.         PK_COLUMN_NAME         = c1.name,
  16371.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16372.         PK_COLUMN_PROPID    = convert(int,null),
  16373.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16374.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16375.         FK_TABLE_NAME         = o2.name,
  16376.         FK_COLUMN_NAME         = c2.name,
  16377.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16378.         FK_COLUMN_PROPID    = convert(int,null),
  16379.         ORDINAL                = convert(int,6),
  16380.         UPDATE_RULE            = N'NO ACTION',
  16381.         DELETE_RULE         = N'NO ACTION',
  16382.         PK_NAME                = object_name(s.constid),
  16383.         FK_NAME                = object_name(r.constid),
  16384.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16385.     from
  16386.         sysobjects o1, sysobjects o2,
  16387.         syscolumns c1, syscolumns c2,
  16388.         sysreferences r, sysconstraints s
  16389.     where    
  16390.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16391.     and    o2.name = @fk_table_name
  16392.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16393.     and    o2.id = r.fkeyid
  16394.     and    r.keycnt >= 6
  16395.     and    o2.id = c2.id
  16396.     and    c2.colid = r.fkey6
  16397.     and r.rkeyid = o1.id
  16398.     and s.id = o1.id and (s.status & 0xf) = 1
  16399.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16400.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16401.     and    o1.id = c1.id    
  16402.     and    c1.colid = r.rkey6
  16403.     union    all
  16404.     select
  16405.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16406.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16407.         PK_TABLE_NAME         = o1.name,
  16408.         PK_COLUMN_NAME         = c1.name,
  16409.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16410.         PK_COLUMN_PROPID    = convert(int,null),
  16411.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16412.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16413.         FK_TABLE_NAME         = o2.name,
  16414.         FK_COLUMN_NAME         = c2.name,
  16415.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16416.         FK_COLUMN_PROPID    = convert(int,null),
  16417.         ORDINAL                = convert(int,7),
  16418.         UPDATE_RULE            = N'NO ACTION',
  16419.         DELETE_RULE         = N'NO ACTION',
  16420.         PK_NAME                = object_name(s.constid),
  16421.         FK_NAME                = object_name(r.constid),
  16422.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16423.     from
  16424.         sysobjects o1, sysobjects o2,
  16425.         syscolumns c1, syscolumns c2,
  16426.         sysreferences r, sysconstraints s
  16427.     where    
  16428.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16429.     and    o2.name = @fk_table_name
  16430.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16431.     and    o2.id = r.fkeyid
  16432.     and    r.keycnt >= 7
  16433.     and    o2.id = c2.id
  16434.     and    c2.colid = r.fkey7
  16435.     and    r.rkeyid = o1.id
  16436.     and s.id = o1.id and (s.status & 0xf) = 1
  16437.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16438.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16439.     and    o1.id = c1.id    
  16440.     and    c1.colid = r.rkey7
  16441.     union    all
  16442.     select
  16443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16445.         PK_TABLE_NAME         = o1.name,
  16446.         PK_COLUMN_NAME         = c1.name,
  16447.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16448.         PK_COLUMN_PROPID    = convert(int,null),
  16449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16450.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16451.         FK_TABLE_NAME         = o2.name,
  16452.         FK_COLUMN_NAME         = c2.name,
  16453.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16454.         FK_COLUMN_PROPID    = convert(int,null),
  16455.         ORDINAL                = convert(int,8),
  16456.         UPDATE_RULE            = N'NO ACTION',
  16457.         DELETE_RULE         = N'NO ACTION',
  16458.         PK_NAME                = object_name(s.constid),
  16459.         FK_NAME                = object_name(r.constid),
  16460.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16461.     from
  16462.         sysobjects o1, sysobjects o2,
  16463.         syscolumns c1, syscolumns c2,
  16464.         sysreferences r, sysconstraints s
  16465.     where    
  16466.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16467.     and    o2.name = @fk_table_name
  16468.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16469.     and    o2.id = r.fkeyid
  16470.     and    r.keycnt >= 8
  16471.     and    o2.id = c2.id
  16472.     and    c2.colid = r.fkey8
  16473.     and r.rkeyid = o1.id
  16474.     and s.id = o1.id and (s.status & 0xf) = 1
  16475.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16476.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16477.     and    o1.id = c1.id    
  16478.     and    c1.colid = r.rkey8
  16479.     union    all
  16480.     select
  16481.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16482.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16483.         PK_TABLE_NAME         = o1.name,
  16484.         PK_COLUMN_NAME         = c1.name,
  16485.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16486.         PK_COLUMN_PROPID    = convert(int,null),
  16487.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16488.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16489.         FK_TABLE_NAME         = o2.name,
  16490.         FK_COLUMN_NAME         = c2.name,
  16491.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16492.         FK_COLUMN_PROPID    = convert(int,null),
  16493.         ORDINAL                = convert(int,9),
  16494.         UPDATE_RULE            = N'NO ACTION',
  16495.         DELETE_RULE         = N'NO ACTION',
  16496.         PK_NAME                = object_name(s.constid),
  16497.         FK_NAME                = object_name(r.constid),
  16498.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16499.     from
  16500.         sysobjects o1, sysobjects o2,
  16501.         syscolumns c1, syscolumns c2,
  16502.         sysreferences r, sysconstraints s
  16503.     where    
  16504.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16505.     and    o2.name = @fk_table_name
  16506.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16507.     and    o2.id = r.fkeyid
  16508.     and    r.keycnt >= 9
  16509.     and    o2.id = c2.id
  16510.     and    c2.colid = r.fkey9
  16511.     and    r.rkeyid = o1.id
  16512.     and s.id = o1.id and (s.status & 0xf) = 1
  16513.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16514.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16515.     and    o1.id = c1.id    
  16516.     and    c1.colid = r.rkey9
  16517.     union    all
  16518.     select
  16519.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16520.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16521.         PK_TABLE_NAME         = o1.name,
  16522.         PK_COLUMN_NAME         = c1.name,
  16523.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16524.         PK_COLUMN_PROPID    = convert(int,null),
  16525.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16526.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16527.         FK_TABLE_NAME         = o2.name,
  16528.         FK_COLUMN_NAME         = c2.name,
  16529.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16530.         FK_COLUMN_PROPID    = convert(int,null),
  16531.         ORDINAL                = convert(int,10),
  16532.         UPDATE_RULE            = N'NO ACTION',
  16533.         DELETE_RULE         = N'NO ACTION',
  16534.         PK_NAME                = object_name(s.constid),
  16535.         FK_NAME                = object_name(r.constid),
  16536.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16537.     from
  16538.         sysobjects o1, sysobjects o2,
  16539.         syscolumns c1, syscolumns c2,
  16540.         sysreferences r, sysconstraints s
  16541.     where    
  16542.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16543.     and    o2.name = @fk_table_name
  16544.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16545.     and    o2.id = r.fkeyid
  16546.     and    r.keycnt >= 10
  16547.     and    o2.id = c2.id
  16548.     and    c2.colid = r.fkey10
  16549.     and r.rkeyid = o1.id
  16550.     and s.id = o1.id and (s.status & 0xf) = 1
  16551.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16552.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16553.     and    o1.id = c1.id    
  16554.     and    c1.colid = r.rkey10
  16555.     union    all
  16556.     select
  16557.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16558.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16559.         PK_TABLE_NAME         = o1.name,
  16560.         PK_COLUMN_NAME         = c1.name,
  16561.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16562.         PK_COLUMN_PROPID    = convert(int,null),
  16563.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16564.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  16565.         FK_TABLE_NAME         = o2.name,
  16566.         FK_COLUMN_NAME         = c2.name,
  16567.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16568.         FK_COLUMN_PROPID    = convert(int,null),
  16569.         ORDINAL                = convert(int,11),
  16570.         UPDATE_RULE            = N'NO ACTION',
  16571.         DELETE_RULE         = N'NO ACTION',
  16572.         PK_NAME                = object_name(s.constid),
  16573.         FK_NAME                = object_name(r.constid),
  16574.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16575.     from
  16576.         sysobjects o1, sysobjects o2,
  16577.         syscolumns c1, syscolumns c2,
  16578.         sysreferences r, sysconstraints s
  16579.     where    
  16580.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16581.     and    o2.name = @fk_table_name
  16582.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16583.     and    o2.id = r.fkeyid
  16584.     and    r.keycnt >= 11
  16585.     and    o2.id = c2.id
  16586.     and    c2.colid = r.fkey11
  16587.     and r.rkeyid = o1.id
  16588.     and s.id = o1.id and (s.status & 0xf) = 1
  16589.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16590.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16591.     and    o1.id = c1.id    
  16592.     and    c1.colid = r.rkey11
  16593.     union    all
  16594.     select
  16595.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16596.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16597.         PK_TABLE_NAME         = o1.name,
  16598.         PK_COLUMN_NAME         = c1.name,
  16599.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16600.         PK_COLUMN_PROPID    = convert(int,null),
  16601.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16602.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16603.         FK_TABLE_NAME         = o2.name,
  16604.         FK_COLUMN_NAME         = c2.name,
  16605.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16606.         FK_COLUMN_PROPID    = convert(int,null),
  16607.         ORDINAL                = convert(int,12),
  16608.         UPDATE_RULE            = N'NO ACTION',
  16609.         DELETE_RULE         = N'NO ACTION',
  16610.         PK_NAME                = object_name(s.constid),
  16611.         FK_NAME                = object_name(r.constid),
  16612.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16613.     from
  16614.         sysobjects o1, sysobjects o2,
  16615.         syscolumns c1, syscolumns c2,
  16616.         sysreferences r, sysconstraints s
  16617.     where    
  16618.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16619.     and    o2.name = @fk_table_name
  16620.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16621.     and    o2.id = r.fkeyid
  16622.     and    r.keycnt >= 12
  16623.     and    o2.id = c2.id
  16624.     and    c2.colid = r.fkey12
  16625.     and r.rkeyid = o1.id
  16626.     and s.id = o1.id and (s.status & 0xf) = 1
  16627.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16628.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16629.     and    o1.id = c1.id    
  16630.     and    c1.colid = r.rkey12
  16631.     union    all
  16632.     select
  16633.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16634.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16635.         PK_TABLE_NAME         = o1.name,
  16636.         PK_COLUMN_NAME         = c1.name,
  16637.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16638.         PK_COLUMN_PROPID    = convert(int,null),
  16639.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16640.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16641.         FK_TABLE_NAME         = o2.name,
  16642.         FK_COLUMN_NAME         = c2.name,
  16643.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16644.         FK_COLUMN_PROPID    = convert(int,null),
  16645.         ORDINAL                = convert(int,13),
  16646.         UPDATE_RULE            = N'NO ACTION',
  16647.         DELETE_RULE         = N'NO ACTION',
  16648.         PK_NAME                = object_name(s.constid),
  16649.         FK_NAME                = object_name(r.constid),
  16650.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16651.     from
  16652.         sysobjects o1, sysobjects o2,
  16653.         syscolumns c1, syscolumns c2,
  16654.         sysreferences r, sysconstraints s
  16655.     where    
  16656.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16657.     and    o2.name = @fk_table_name
  16658.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16659.     and    o2.id = r.fkeyid
  16660.     and    r.keycnt >= 13
  16661.     and    o2.id = c2.id
  16662.     and    c2.colid = r.fkey13
  16663.     and r.rkeyid = o1.id
  16664.     and s.id = o1.id and (s.status & 0xf) = 1
  16665.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16666.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16667.     and    o1.id = c1.id    
  16668.     and    c1.colid = r.rkey13
  16669.     union    all
  16670.     select
  16671.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16672.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16673.         PK_TABLE_NAME         = o1.name,
  16674.         PK_COLUMN_NAME         = c1.name,
  16675.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16676.         PK_COLUMN_PROPID    = convert(int,null),
  16677.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16678.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16679.         FK_TABLE_NAME         = o2.name,
  16680.         FK_COLUMN_NAME         = c2.name,
  16681.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16682.         FK_COLUMN_PROPID    = convert(int,null),
  16683.         ORDINAL                = convert(int,14),
  16684.         UPDATE_RULE            = N'NO ACTION',
  16685.         DELETE_RULE         = N'NO ACTION',
  16686.         PK_NAME                = object_name(s.constid),
  16687.         FK_NAME                = object_name(r.constid),
  16688.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16689.     from
  16690.         sysobjects o1, sysobjects o2,
  16691.         syscolumns c1, syscolumns c2,
  16692.         sysreferences r, sysconstraints s
  16693.     where    
  16694.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16695.     and    o2.name = @fk_table_name
  16696.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16697.     and    o2.id = r.fkeyid
  16698.     and    r.keycnt >= 14
  16699.     and    o2.id = c2.id
  16700.     and    c2.colid = r.fkey14
  16701.     and r.rkeyid = o1.id
  16702.     and s.id = o1.id and (s.status & 0xf) = 1
  16703.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16704.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16705.     and    o1.id = c1.id    
  16706.     and    c1.colid = r.rkey14
  16707.     union    all
  16708.     select
  16709.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16710.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16711.         PK_TABLE_NAME         = o1.name,
  16712.         PK_COLUMN_NAME         = c1.name,
  16713.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16714.         PK_COLUMN_PROPID    = convert(int,null),
  16715.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16716.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16717.         FK_TABLE_NAME         = o2.name,
  16718.         FK_COLUMN_NAME         = c2.name,
  16719.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16720.         FK_COLUMN_PROPID    = convert(int,null),
  16721.         ORDINAL                = convert(int,15),
  16722.         UPDATE_RULE            = N'NO ACTION',
  16723.         DELETE_RULE         = N'NO ACTION',
  16724.         PK_NAME                = object_name(s.constid),
  16725.         FK_NAME                = object_name(r.constid),
  16726.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16727.     from
  16728.         sysobjects o1, sysobjects o2,
  16729.         syscolumns c1, syscolumns c2,
  16730.         sysreferences r, sysconstraints s
  16731.     where    
  16732.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16733.     and    o2.name = @fk_table_name
  16734.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16735.     and    o2.id = r.fkeyid
  16736.     and    r.keycnt >= 15
  16737.     and    o2.id = c2.id
  16738.     and    c2.colid = r.fkey15
  16739.     and r.rkeyid = o1.id
  16740.     and s.id = o1.id and (s.status & 0xf) = 1
  16741.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16742.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16743.     and    o1.id = c1.id    
  16744.     and    c1.colid = r.rkey15
  16745.     union    all
  16746.     select
  16747.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16748.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16749.         PK_TABLE_NAME         = o1.name,
  16750.         PK_COLUMN_NAME         = c1.name,
  16751.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16752.         PK_COLUMN_PROPID    = convert(int,null),
  16753.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16754.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16755.         FK_TABLE_NAME         = o2.name,
  16756.         FK_COLUMN_NAME         = c2.name,
  16757.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16758.         FK_COLUMN_PROPID    = convert(int,null),
  16759.         ORDINAL                = convert(int,16),
  16760.         UPDATE_RULE            = N'NO ACTION',
  16761.         DELETE_RULE         = N'NO ACTION',
  16762.         PK_NAME                = object_name(s.constid),
  16763.         FK_NAME                = object_name(r.constid),
  16764.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16765.     from
  16766.         sysobjects o1, sysobjects o2,
  16767.         syscolumns c1, syscolumns c2,
  16768.         sysreferences r, sysconstraints s
  16769.     where    
  16770.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16771.     and    o2.name = @fk_table_name
  16772.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16773.     and    o2.id = r.fkeyid
  16774.     and    r.keycnt >= 16
  16775.     and    o2.id = c2.id
  16776.     and    c2.colid = r.fkey16
  16777.     and r.rkeyid = o1.id
  16778.     and s.id = o1.id and (s.status & 0xf) = 1
  16779.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16780.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16781.     and    o1.id = c1.id    
  16782.     and    c1.colid = r.rkey16
  16783.     order by 8,9,2,3,13
  16784. go
  16785. dump tran master with no_log
  16786. go
  16787. CREATE PROCEDURE sp_foreign_keys_rowset;3
  16788.     (
  16789.     @pk_table_schema    sysname = null,
  16790.     @pk_table_catalog    sysname = null,
  16791.     @fk_table_schema    sysname = null,
  16792.     @fk_table_catalog    sysname = null
  16793.     )
  16794. as
  16795.     select
  16796.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16797.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16798.         PK_TABLE_NAME         = o1.name,
  16799.         PK_COLUMN_NAME         = c1.name,
  16800.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16801.         PK_COLUMN_PROPID    = convert(int,null),
  16802.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16803.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16804.         FK_TABLE_NAME         = o2.name,
  16805.         FK_COLUMN_NAME         = c2.name,
  16806.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16807.         FK_COLUMN_PROPID    = convert(int,null),
  16808.         ORDINAL                = convert(int,1),
  16809.         UPDATE_RULE            = N'NO ACTION',
  16810.         DELETE_RULE         = N'NO ACTION',
  16811.         PK_NAME                = object_name(s.constid),
  16812.         FK_NAME                = object_name(r.constid),
  16813.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  16814.     from
  16815.         sysobjects o1, sysobjects o2,
  16816.         syscolumns c1, syscolumns c2,
  16817.         sysreferences r, sysconstraints s
  16818.     where    
  16819.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16820.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16821.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16822.     and    o1.id = r.rkeyid
  16823.     and s.id = o1.id and (s.status & 0xf) = 1
  16824.     and    o1.id = c1.id
  16825.     and    c1.colid = r.rkey1
  16826.     and r.fkeyid = o2.id
  16827.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16828.     and    o2.id = c2.id    
  16829.     and    c2.colid = r.fkey1
  16830.     union all
  16831.     select
  16832.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16833.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16834.         PK_TABLE_NAME         = o1.name,
  16835.         PK_COLUMN_NAME         = c1.name,
  16836.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16837.         PK_COLUMN_PROPID    = convert(int,null),
  16838.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16839.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16840.         FK_TABLE_NAME         = o2.name,
  16841.         FK_COLUMN_NAME         = c2.name,
  16842.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16843.         FK_COLUMN_PROPID    = convert(int,null),
  16844.         ORDINAL                = convert(int,2),
  16845.         UPDATE_RULE            = N'NO ACTION',
  16846.         DELETE_RULE         = N'NO ACTION',
  16847.         PK_NAME                = object_name(s.constid),
  16848.         FK_NAME                = object_name(r.constid),
  16849.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16850.     from
  16851.         sysobjects o1, sysobjects o2,
  16852.         syscolumns c1, syscolumns c2,
  16853.         sysreferences r, sysconstraints s
  16854.     where    
  16855.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16856.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16857.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16858.     and    o1.id = r.rkeyid
  16859.     and s.id = o1.id and (s.status & 0xf) = 1
  16860.     and    r.keycnt >= 2
  16861.     and    o1.id = c1.id
  16862.     and    c1.colid = r.rkey2
  16863.     and r.fkeyid = o2.id
  16864.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16865.     and    o2.id = c2.id    
  16866.     and    c2.colid = r.fkey2
  16867.     union all
  16868.     select
  16869.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16870.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16871.         PK_TABLE_NAME         = o1.name,
  16872.         PK_COLUMN_NAME         = c1.name,
  16873.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16874.         PK_COLUMN_PROPID    = convert(int,null),
  16875.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16876.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16877.         FK_TABLE_NAME         = o2.name,
  16878.         FK_COLUMN_NAME         = c2.name,
  16879.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16880.         FK_COLUMN_PROPID    = convert(int,null),
  16881.         ORDINAL                = convert(int,3),
  16882.         UPDATE_RULE            = N'NO ACTION',
  16883.         DELETE_RULE         = N'NO ACTION',
  16884.         PK_NAME                = object_name(s.constid),
  16885.         FK_NAME                = object_name(r.constid),
  16886.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16887.     from
  16888.         sysobjects o1, sysobjects o2,
  16889.         syscolumns c1, syscolumns c2,
  16890.         sysreferences r, sysconstraints s
  16891.     where    
  16892.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16893.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16894.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16895.     and    o1.id = r.rkeyid
  16896.     and s.id = o1.id and (s.status & 0xf) = 1
  16897.     and    r.keycnt >= 3
  16898.     and    o1.id = c1.id
  16899.     and    c1.colid = r.rkey3
  16900.     and r.fkeyid = o2.id
  16901.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16902.     and    o2.id = c2.id    
  16903.     and    c2.colid = r.fkey3
  16904.     union all
  16905.     select
  16906.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16907.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16908.         PK_TABLE_NAME         = o1.name,
  16909.         PK_COLUMN_NAME         = c1.name,
  16910.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16911.         PK_COLUMN_PROPID    = convert(int,null),
  16912.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16913.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16914.         FK_TABLE_NAME         = o2.name,
  16915.         FK_COLUMN_NAME         = c2.name,
  16916.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16917.         FK_COLUMN_PROPID    = convert(int,null),
  16918.         ORDINAL                = convert(int,4),
  16919.         UPDATE_RULE            = N'NO ACTION',
  16920.         DELETE_RULE         = N'NO ACTION',
  16921.         PK_NAME                = object_name(s.constid),
  16922.         FK_NAME                = object_name(r.constid),
  16923.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16924.     from
  16925.         sysobjects o1, sysobjects o2,
  16926.         syscolumns c1, syscolumns c2,
  16927.         sysreferences r, sysconstraints s
  16928.     where    
  16929.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16930.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16931.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16932.     and    o1.id = r.rkeyid
  16933.     and s.id = o1.id and (s.status & 0xf) = 1
  16934.     and    r.keycnt >= 4
  16935.     and    o1.id = c1.id
  16936.     and    c1.colid = r.rkey4
  16937.     and r.fkeyid = o2.id
  16938.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16939.     and    o2.id = c2.id    
  16940.     and    c2.colid = r.fkey4
  16941.     union all
  16942.     select
  16943.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16944.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16945.         PK_TABLE_NAME         = o1.name,
  16946.         PK_COLUMN_NAME         = c1.name,
  16947.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16948.         PK_COLUMN_PROPID    = convert(int,null),
  16949.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16950.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16951.         FK_TABLE_NAME         = o2.name,
  16952.         FK_COLUMN_NAME         = c2.name,
  16953.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16954.         FK_COLUMN_PROPID    = convert(int,null),
  16955.         ORDINAL                = convert(int,5),
  16956.         UPDATE_RULE            = N'NO ACTION',
  16957.         DELETE_RULE         = N'NO ACTION',
  16958.         PK_NAME                = object_name(s.constid),
  16959.         FK_NAME                = object_name(r.constid),
  16960.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16961.     from
  16962.         sysobjects o1, sysobjects o2,
  16963.         syscolumns c1, syscolumns c2,
  16964.         sysreferences r, sysconstraints s
  16965.     where    
  16966.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16967.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16968.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16969.     and    o1.id = r.rkeyid
  16970.     and s.id = o1.id and (s.status & 0xf) = 1
  16971.     and    r.keycnt >= 5
  16972.     and    o1.id = c1.id
  16973.     and    c1.colid = r.rkey5
  16974.     and r.fkeyid = o2.id
  16975.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16976.     and    o2.id = c2.id    
  16977.     and    c2.colid = r.fkey5
  16978.     union all
  16979.     select
  16980.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16981.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16982.         PK_TABLE_NAME         = o1.name,
  16983.         PK_COLUMN_NAME         = c1.name,
  16984.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16985.         PK_COLUMN_PROPID    = convert(int,null),
  16986.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16987.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16988.         FK_TABLE_NAME         = o2.name,
  16989.         FK_COLUMN_NAME         = c2.name,
  16990.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16991.         FK_COLUMN_PROPID    = convert(int,null),
  16992.         ORDINAL                = convert(int,6),
  16993.         UPDATE_RULE            = N'NO ACTION',
  16994.         DELETE_RULE         = N'NO ACTION',
  16995.         PK_NAME                = object_name(s.constid),
  16996.         FK_NAME                = object_name(r.constid),
  16997.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16998.     from
  16999.         sysobjects o1, sysobjects o2,
  17000.         syscolumns c1, syscolumns c2,
  17001.         sysreferences r, sysconstraints s
  17002.     where    
  17003.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17004.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17005.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17006.     and    o1.id = r.rkeyid
  17007.     and s.id = o1.id and (s.status & 0xf) = 1
  17008.     and    r.keycnt >= 6
  17009.     and    o1.id = c1.id
  17010.     and    c1.colid = r.rkey6
  17011.     and r.fkeyid = o2.id
  17012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17013.     and    o2.id = c2.id    
  17014.     and    c2.colid = r.fkey6
  17015.     union all
  17016.     select
  17017.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17018.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17019.         PK_TABLE_NAME         = o1.name,
  17020.         PK_COLUMN_NAME         = c1.name,
  17021.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17022.         PK_COLUMN_PROPID    = convert(int,null),
  17023.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17024.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17025.         FK_TABLE_NAME         = o2.name,
  17026.         FK_COLUMN_NAME         = c2.name,
  17027.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17028.         FK_COLUMN_PROPID    = convert(int,null),
  17029.         ORDINAL                = convert(int,7),
  17030.         UPDATE_RULE            = N'NO ACTION',
  17031.         DELETE_RULE         = N'NO ACTION',
  17032.         PK_NAME                = object_name(s.constid),
  17033.         FK_NAME                = object_name(r.constid),
  17034.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17035.     from
  17036.         sysobjects o1, sysobjects o2,
  17037.         syscolumns c1, syscolumns c2,
  17038.         sysreferences r, sysconstraints s
  17039.     where    
  17040.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17041.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17042.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17043.     and    o1.id = r.rkeyid
  17044.     and s.id = o1.id and (s.status & 0xf) = 1
  17045.     and    r.keycnt >= 7
  17046.     and    o1.id = c1.id
  17047.     and    c1.colid = r.rkey7
  17048.     and r.fkeyid = o2.id
  17049.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17050.     and    o2.id = c2.id    
  17051.     and    c2.colid = r.fkey7
  17052.     union all
  17053.     select
  17054.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17055.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17056.         PK_TABLE_NAME         = o1.name,
  17057.         PK_COLUMN_NAME         = c1.name,
  17058.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17059.         PK_COLUMN_PROPID    = convert(int,null),
  17060.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17061.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17062.         FK_TABLE_NAME         = o2.name,
  17063.         FK_COLUMN_NAME         = c2.name,
  17064.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17065.         FK_COLUMN_PROPID    = convert(int,null),
  17066.         ORDINAL                = convert(int,8),
  17067.         UPDATE_RULE            = N'NO ACTION',
  17068.         DELETE_RULE         = N'NO ACTION',
  17069.         PK_NAME                = object_name(s.constid),
  17070.         FK_NAME                = object_name(r.constid),
  17071.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17072.     from
  17073.         sysobjects o1, sysobjects o2,
  17074.         syscolumns c1, syscolumns c2,
  17075.         sysreferences r, sysconstraints s
  17076.     where    
  17077.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17078.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17079.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17080.     and    o1.id = r.rkeyid
  17081.     and s.id = o1.id and (s.status & 0xf) = 1
  17082.     and    r.keycnt >= 8
  17083.     and    o1.id = c1.id
  17084.     and    c1.colid = r.rkey8
  17085.     and r.fkeyid = o2.id
  17086.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17087.     and    o2.id = c2.id    
  17088.     and    c2.colid = r.fkey8
  17089.     union all
  17090.     select
  17091.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17092.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17093.         PK_TABLE_NAME         = o1.name,
  17094.         PK_COLUMN_NAME         = c1.name,
  17095.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17096.         PK_COLUMN_PROPID    = convert(int,null),
  17097.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17098.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17099.         FK_TABLE_NAME         = o2.name,
  17100.         FK_COLUMN_NAME         = c2.name,
  17101.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17102.         FK_COLUMN_PROPID    = convert(int,null),
  17103.         ORDINAL                = convert(int,9),
  17104.         UPDATE_RULE            = N'NO ACTION',
  17105.         DELETE_RULE         = N'NO ACTION',
  17106.         PK_NAME                = object_name(s.constid),
  17107.         FK_NAME                = object_name(r.constid),
  17108.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17109.     from
  17110.         sysobjects o1, sysobjects o2,
  17111.         syscolumns c1, syscolumns c2,
  17112.         sysreferences r, sysconstraints s
  17113.     where    
  17114.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17115.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17116.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17117.     and    o1.id = r.rkeyid
  17118.     and s.id = o1.id and (s.status & 0xf) = 1
  17119.     and    r.keycnt >= 9
  17120.     and    o1.id = c1.id
  17121.     and    c1.colid = r.rkey9
  17122.     and r.fkeyid = o2.id
  17123.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17124.     and    o2.id = c2.id    
  17125.     and    c2.colid = r.fkey9
  17126.     union all
  17127.     select
  17128.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17129.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17130.         PK_TABLE_NAME         = o1.name,
  17131.         PK_COLUMN_NAME         = c1.name,
  17132.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17133.         PK_COLUMN_PROPID    = convert(int,null),
  17134.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17135.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17136.         FK_TABLE_NAME         = o2.name,
  17137.         FK_COLUMN_NAME         = c2.name,
  17138.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17139.         FK_COLUMN_PROPID    = convert(int,null),
  17140.         ORDINAL                = convert(int,10),
  17141.         UPDATE_RULE            = N'NO ACTION',
  17142.         DELETE_RULE         = N'NO ACTION',
  17143.         PK_NAME                = object_name(s.constid),
  17144.         FK_NAME                = object_name(r.constid),
  17145.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17146.     from
  17147.         sysobjects o1, sysobjects o2,
  17148.         syscolumns c1, syscolumns c2,
  17149.         sysreferences r, sysconstraints s
  17150.     where    
  17151.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17152.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17153.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17154.     and    o1.id = r.rkeyid
  17155.     and s.id = o1.id and (s.status & 0xf) = 1
  17156.     and    r.keycnt >= 10
  17157.     and    o1.id = c1.id
  17158.     and    c1.colid = r.rkey10
  17159.     and r.fkeyid = o2.id
  17160.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17161.     and    o2.id = c2.id    
  17162.     and    c2.colid = r.fkey10
  17163.     union all
  17164.     select
  17165.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17166.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17167.         PK_TABLE_NAME         = o1.name,
  17168.         PK_COLUMN_NAME         = c1.name,
  17169.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17170.         PK_COLUMN_PROPID    = convert(int,null),
  17171.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17172.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17173.         FK_TABLE_NAME         = o2.name,
  17174.         FK_COLUMN_NAME         = c2.name,
  17175.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17176.         FK_COLUMN_PROPID    = convert(int,null),
  17177.         ORDINAL                = convert(int,11),
  17178.         UPDATE_RULE            = N'NO ACTION',
  17179.         DELETE_RULE         = N'NO ACTION',
  17180.         PK_NAME                = object_name(s.constid),
  17181.         FK_NAME                = object_name(r.constid),
  17182.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17183.     from
  17184.         sysobjects o1, sysobjects o2,
  17185.         syscolumns c1, syscolumns c2,
  17186.         sysreferences r, sysconstraints s
  17187.     where    
  17188.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17189.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17190.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17191.     and    o1.id = r.rkeyid
  17192.     and s.id = o1.id and (s.status & 0xf) = 1
  17193.     and    r.keycnt >= 11
  17194.     and    o1.id = c1.id
  17195.     and    c1.colid = r.rkey11
  17196.     and r.fkeyid = o2.id
  17197.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17198.     and    o2.id = c2.id    
  17199.     and    c2.colid = r.fkey11
  17200.     union all
  17201.     select
  17202.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17203.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17204.         PK_TABLE_NAME         = o1.name,
  17205.         PK_COLUMN_NAME         = c1.name,
  17206.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17207.         PK_COLUMN_PROPID    = convert(int,null),
  17208.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17209.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17210.         FK_TABLE_NAME         = o2.name,
  17211.         FK_COLUMN_NAME         = c2.name,
  17212.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17213.         FK_COLUMN_PROPID    = convert(int,null),
  17214.         ORDINAL                = convert(int,12),
  17215.         UPDATE_RULE            = N'NO ACTION',
  17216.         DELETE_RULE         = N'NO ACTION',
  17217.         PK_NAME                = object_name(s.constid),
  17218.         FK_NAME                = object_name(r.constid),
  17219.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17220.     from
  17221.         sysobjects o1, sysobjects o2,
  17222.         syscolumns c1, syscolumns c2,
  17223.         sysreferences r, sysconstraints s
  17224.     where    
  17225.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17226.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17227.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17228.     and    o1.id = r.rkeyid
  17229.     and s.id = o1.id and (s.status & 0xf) = 1
  17230.     and    r.keycnt >= 12
  17231.     and    o1.id = c1.id
  17232.     and    c1.colid = r.rkey12
  17233.     and r.fkeyid = o2.id
  17234.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17235.     and    o2.id = c2.id    
  17236.     and    c2.colid = r.fkey12
  17237.     union all
  17238.     select
  17239.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17240.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17241.         PK_TABLE_NAME         = o1.name,
  17242.         PK_COLUMN_NAME         = c1.name,
  17243.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17244.         PK_COLUMN_PROPID    = convert(int,null),
  17245.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17246.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17247.         FK_TABLE_NAME         = o2.name,
  17248.         FK_COLUMN_NAME         = c2.name,
  17249.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17250.         FK_COLUMN_PROPID    = convert(int,null),
  17251.         ORDINAL                = convert(int,13),
  17252.         UPDATE_RULE            = N'NO ACTION',
  17253.         DELETE_RULE         = N'NO ACTION',
  17254.         PK_NAME                = object_name(s.constid),
  17255.         FK_NAME                = object_name(r.constid),
  17256.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17257.     from
  17258.         sysobjects o1, sysobjects o2,
  17259.         syscolumns c1, syscolumns c2,
  17260.         sysreferences r, sysconstraints s
  17261.     where    
  17262.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17263.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17264.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17265.     and    o1.id = r.rkeyid
  17266.     and s.id = o1.id and (s.status & 0xf) = 1
  17267.     and    r.keycnt >= 13
  17268.     and    o1.id = c1.id
  17269.     and    c1.colid = r.rkey13
  17270.     and r.fkeyid = o2.id
  17271.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17272.     and    o2.id = c2.id    
  17273.     and    c2.colid = r.fkey13
  17274.     union all
  17275.     select
  17276.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17277.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17278.         PK_TABLE_NAME         = o1.name,
  17279.         PK_COLUMN_NAME         = c1.name,
  17280.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17281.         PK_COLUMN_PROPID    = convert(int,null),
  17282.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17283.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17284.         FK_TABLE_NAME         = o2.name,
  17285.         FK_COLUMN_NAME         = c2.name,
  17286.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17287.         FK_COLUMN_PROPID    = convert(int,null),
  17288.         ORDINAL                = convert(int,14),
  17289.         UPDATE_RULE            = N'NO ACTION',
  17290.         DELETE_RULE         = N'NO ACTION',
  17291.         PK_NAME                = object_name(s.constid),
  17292.         FK_NAME                = object_name(r.constid),
  17293.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17294.     from
  17295.         sysobjects o1, sysobjects o2,
  17296.         syscolumns c1, syscolumns c2,
  17297.         sysreferences r, sysconstraints s
  17298.     where    
  17299.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17300.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17301.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17302.     and    o1.id = r.rkeyid
  17303.     and s.id = o1.id and (s.status & 0xf) = 1
  17304.     and    r.keycnt >= 14
  17305.     and    o1.id = c1.id
  17306.     and    c1.colid = r.rkey14
  17307.     and r.fkeyid = o2.id
  17308.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17309.     and    o2.id = c2.id    
  17310.     and    c2.colid = r.fkey14
  17311.     union all
  17312.     select
  17313.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17314.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17315.         PK_TABLE_NAME         = o1.name,
  17316.         PK_COLUMN_NAME         = c1.name,
  17317.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17318.         PK_COLUMN_PROPID    = convert(int,null),
  17319.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17320.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17321.         FK_TABLE_NAME         = o2.name,
  17322.         FK_COLUMN_NAME         = c2.name,
  17323.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17324.         FK_COLUMN_PROPID    = convert(int,null),
  17325.         ORDINAL                = convert(int,15),
  17326.         UPDATE_RULE            = N'NO ACTION',
  17327.         DELETE_RULE         = N'NO ACTION',
  17328.         PK_NAME                = object_name(s.constid),
  17329.         FK_NAME                = object_name(r.constid),
  17330.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17331.     from
  17332.         sysobjects o1, sysobjects o2,
  17333.         syscolumns c1, syscolumns c2,
  17334.         sysreferences r, sysconstraints s
  17335.     where    
  17336.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17337.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17338.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17339.     and    o1.id = r.rkeyid
  17340.     and s.id = o1.id and (s.status & 0xf) = 1
  17341.     and    r.keycnt >= 15
  17342.     and    o1.id = c1.id
  17343.     and    c1.colid = r.rkey15
  17344.     and r.fkeyid = o2.id
  17345.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17346.     and    o2.id = c2.id    
  17347.     and    c2.colid = r.fkey15
  17348.     union all
  17349.     select
  17350.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17351.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17352.         PK_TABLE_NAME         = o1.name,
  17353.         PK_COLUMN_NAME         = c1.name,
  17354.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17355.         PK_COLUMN_PROPID    = convert(int,null),
  17356.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17357.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17358.         FK_TABLE_NAME         = o2.name,
  17359.         FK_COLUMN_NAME         = c2.name,
  17360.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17361.         FK_COLUMN_PROPID    = convert(int,null),
  17362.         ORDINAL                = convert(int,16),
  17363.         UPDATE_RULE            = N'NO ACTION',
  17364.         DELETE_RULE         = N'NO ACTION',
  17365.         PK_NAME                = object_name(s.constid),
  17366.         FK_NAME                = object_name(r.constid),
  17367.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17368.     from
  17369.         sysobjects o1, sysobjects o2,
  17370.         syscolumns c1, syscolumns c2,
  17371.         sysreferences r, sysconstraints s
  17372.     where    
  17373.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17374.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17375.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17376.     and    o1.id = r.rkeyid
  17377.     and s.id = o1.id and (s.status & 0xf) = 1
  17378.     and    o1.id = c1.id
  17379.     and    r.keycnt >= 16
  17380.     and    c1.colid = r.rkey16
  17381.     and r.fkeyid = o2.id
  17382.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17383.     and    o2.id = c2.id    
  17384.     and    c2.colid = r.fkey16
  17385.     order by 8,9,2,3,13
  17386. go
  17387. dump tran master with no_log
  17388. go
  17389. create procedure sp_foreign_keys_rowset;5
  17390.     (
  17391.     @server_name        sysname,
  17392.     @pk_catalog_name    sysname = null,
  17393.     @fk_catalog_name    sysname = null,
  17394.     @pk_table_name        sysname = null,
  17395.     @pk_table_schema    sysname = null,
  17396.     @fk_table_name        sysname = null,
  17397.     @fk_table_schema    sysname = null
  17398.     )
  17399. as
  17400.     select
  17401.         PK_TABLE_CATALOG,
  17402.         PK_TABLE_SCHEMA,    
  17403.         PK_TABLE_NAME,     
  17404.         PK_COLUMN_NAME,     
  17405.         PK_COLUMN_GUID,    
  17406.         PK_COLUMN_PROPID,
  17407.         FK_TABLE_CATALOG,
  17408.         FK_TABLE_SCHEMA, 
  17409.         FK_TABLE_NAME,     
  17410.         FK_COLUMN_NAME,     
  17411.         FK_COLUMN_GUID,    
  17412.         FK_COLUMN_PROPID,
  17413.         ORDINAL,        
  17414.         UPDATE_RULE,    
  17415.         DELETE_RULE
  17416.         --PK_NAME,        
  17417.         --FK_NAME,
  17418.         --DEFERRABILITY    
  17419.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  17420.                 @server_name,
  17421.                 @pk_catalog_name,
  17422.                 @pk_table_schema,
  17423.                 @pk_table_name,
  17424.                 @fk_catalog_name,
  17425.                 @fk_table_schema,
  17426.                 @fk_table_name >
  17427.     order by 7,8,9,1,2,3,13
  17428. go
  17429.  
  17430. grant execute on sp_foreign_keys_rowset to public
  17431. go
  17432.  
  17433. dump tran master with no_log
  17434. go
  17435. if (charindex('6.00', @@version) > 0)
  17436.     begin
  17437.     if (exists (select * from sysobjects
  17438.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  17439.         begin
  17440.         drop procedure sp_foreign_keys_rowset
  17441.         dump tran master with no_log
  17442.         end
  17443.     end
  17444. go
  17445.  
  17446.  
  17447. print ''
  17448. print 'creating sp_indexes_rowset'
  17449. go
  17450.  
  17451. /*    6.0 and 6.5 version */
  17452. create procedure sp_indexes_rowset
  17453.     (
  17454.     @table_name    varchar(255), 
  17455.     @index_name    varchar(255) = null,
  17456.     @table_schema    varchar(255) = null     
  17457.     )
  17458. as
  17459.     select    TABLE_CATALOG        = db_name(),
  17460.         TABLE_SCHEMA        = user_name(o.uid),
  17461.         TABLE_NAME        = o.name,
  17462.         INDEX_CATALOG        = db_name(),        
  17463.         INDEX_SCHEMA        = user_name(o.uid),
  17464.         INDEX_NAME        = x.name,
  17465.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17466.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17467.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17468.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17469.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17470.         INITIAL_SIZE        = convert(int,null),
  17471.         NULLS            = convert(int,null),
  17472.         SORT_BOOKMARKS        = convert(bit,0),
  17473.         AUTO_UPDATE        = convert(bit,1),
  17474.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17475.         ORDINAL_POSITION    = convert(int,c.colid),
  17476.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17477.         COLUMN_GUID        = convert(binary(16),null),
  17478.         COLUMN_PROPID        = convert(int,null),
  17479.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17480.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17481.         PAGES            = convert(int, x.dpages),
  17482.         FILTER_CONDITION    = convert(varchar(1),null),
  17483.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  17484.         
  17485.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  17486.     where    o.type in ('U')
  17487.     and     o.name = @table_name
  17488.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17489.     and    (@index_name is null or @index_name = x.name)
  17490.     and     x.id = o.id
  17491.     and     x.id = c.id
  17492.     and      c.colid < x.keycnt+(x.status&16)/16
  17493.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17494.     and     (
  17495.         suser_id() = 1     /* User is the System Administrator */
  17496.         or o.uid = user_id()     /* User created the object */
  17497.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17498.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17499.             from sysprotects p
  17500.             where p.id = o.id
  17501.             /* get rows for public,current user,user's group */
  17502.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17503.             /* check for SELECT,EXECUTE privilege */
  17504.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17505.             ) = 1    /* final magic...compare Grants      */
  17506.            )
  17507.     order by 8 desc, 4, 5, 6, 17
  17508. go
  17509. dump tran master with no_log
  17510. go
  17511. create procedure sp_indexes_rowset;2
  17512.     (
  17513.     @index_name    varchar(255) = null,
  17514.     @table_schema    varchar(255) = null     
  17515.     )
  17516. as
  17517.     select    TABLE_CATALOG        = db_name(),
  17518.         TABLE_SCHEMA        = user_name(o.uid),
  17519.         TABLE_NAME        = o.name,
  17520.         INDEX_CATALOG        = db_name(),        
  17521.         INDEX_SCHEMA        = user_name(o.uid),
  17522.         INDEX_NAME        = x.name,
  17523.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17524.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17525.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17526.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17527.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17528.         INITIAL_SIZE        = convert(int,null),
  17529.         NULLS            = convert(int,null),
  17530.         SORT_BOOKMARKS        = convert(bit,0),
  17531.         AUTO_UPDATE        = convert(bit,1),
  17532.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17533.         ORDINAL_POSITION    = convert(int,c.colid),
  17534.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17535.         COLUMN_GUID        = convert(binary(16),null),
  17536.         COLUMN_PROPID        = convert(int,null),
  17537.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17538.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17539.         PAGES            = convert(int, x.dpages),
  17540.         FILTER_CONDITION    = convert(varchar(1),null),
  17541.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17542.         
  17543.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  17544.     where    o.type in ('U')
  17545.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17546.     and    (@index_name is null or @index_name = x.name)
  17547.     and     x.id = o.id
  17548.     and     x.id = c.id
  17549.     and      c.colid < x.keycnt+(x.status&16)/16
  17550.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17551.     and     (
  17552.         suser_id() = 1     /* User is the System Administrator */
  17553.         or o.uid = user_id()     /* User created the object */
  17554.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17555.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17556.             from sysprotects p
  17557.             where p.id = o.id
  17558.             /* get rows for public,current user,user's group */
  17559.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17560.             /* check for SELECT,EXECUTE privilege */
  17561.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17562.             ) = 1    /* final magic...compare Grants      */
  17563.            )
  17564.     order by 8 desc, 4, 5, 6, 17
  17565. go
  17566. dump tran master with no_log
  17567. go
  17568.  
  17569.  
  17570. if (charindex('7.00', @@version) > 0)
  17571.     drop procedure sp_indexes_rowset
  17572. else
  17573. begin
  17574.     print ''
  17575.     print ''
  17576.     print 'Warning:'
  17577.     print 'you are installing the stored procedures '
  17578.     print 'on a pre 7.0 SQL Server.'
  17579.     print 'Ignore the following errors.'
  17580. end
  17581. go
  17582.  
  17583. /*     7.0 version */
  17584. create procedure sp_indexes_rowset
  17585.     (
  17586.     @table_name    sysname, 
  17587.     @index_name    sysname = null,
  17588.     @table_schema    sysname = null     
  17589.     )
  17590. as
  17591.     select    TABLE_CATALOG        = db_name(),
  17592.         TABLE_SCHEMA        = user_name(o.uid),
  17593.         TABLE_NAME        = o.name,
  17594.         INDEX_CATALOG        = db_name(),        
  17595.         INDEX_SCHEMA        = user_name(o.uid),
  17596.         INDEX_NAME        = x.name,
  17597.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17598.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17599.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17600.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17601.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17602.         INITIAL_SIZE        = convert(int,null),
  17603.         NULLS            = convert(int,null),
  17604.         SORT_BOOKMARKS        = convert(bit,0),
  17605.         AUTO_UPDATE        = convert(bit,1),
  17606.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17607.         ORDINAL_POSITION    = convert(int,c.colid),
  17608.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17609.         COLUMN_GUID        = convert(uniqueidentifier,null),
  17610.         COLUMN_PROPID        = convert(int,null),
  17611.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17612.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17613.         PAGES            = convert(int, x.dpages),
  17614.         FILTER_CONDITION    = convert(nvarchar(1),null),
  17615.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17616.         
  17617.     from    sysobjects o, sysindexes x, syscolumns c
  17618.     where    o.type in ('U')
  17619.     and     o.name = @table_name
  17620.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17621.     and    (@index_name is null or @index_name = x.name)
  17622.     and     x.id = o.id
  17623.     and     x.id = c.id
  17624.     and      c.colid < x.keycnt+(x.status&16)/16
  17625.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  17626.     and     permissions(o.id) <> 0
  17627.     and     (x.status&32) = 0  -- No hypothetical indexes
  17628.     order by 8 desc, 4, 5, 6, 17
  17629. go
  17630. dump tran master with no_log
  17631. go
  17632. create procedure sp_indexes_rowset;2
  17633.     (
  17634.     @index_name    sysname = null,
  17635.     @table_schema    sysname = null     
  17636.     )
  17637. as
  17638.     select    TABLE_CATALOG        = db_name(),
  17639.         TABLE_SCHEMA        = user_name(o.uid),
  17640.         TABLE_NAME        = o.name,
  17641.         INDEX_CATALOG        = db_name(),        
  17642.         INDEX_SCHEMA        = user_name(o.uid),
  17643.         INDEX_NAME        = x.name,
  17644.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  17645.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  17646.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  17647.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  17648.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  17649.         INITIAL_SIZE        = convert(int,null),
  17650.         NULLS            = convert(int,null),
  17651.         SORT_BOOKMARKS        = convert(bit,0),
  17652.         AUTO_UPDATE        = convert(bit,1),
  17653.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  17654.         ORDINAL_POSITION    = convert(int,c.colid),
  17655.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  17656.         COLUMN_GUID        = convert(uniqueidentifier,null),
  17657.         COLUMN_PROPID        = convert(int,null),
  17658.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  17659.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  17660.         PAGES            = convert(int, x.dpages),
  17661.         FILTER_CONDITION    = convert(nvarchar(1),null),
  17662.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  17663.         
  17664.     from    sysobjects o, sysindexes x, syscolumns c
  17665.     where    o.type in ('U')
  17666.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17667.     and    (@index_name is null or @index_name = x.name)
  17668.     and     x.id = o.id
  17669.     and     x.id = c.id
  17670.     and      c.colid < x.keycnt+(x.status&16)/16
  17671.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  17672.     and     permissions(o.id) <> 0
  17673.     and     (x.status&32) = 0  -- No hypothetical indexes
  17674.     order by 8 desc, 4, 5, 6, 17
  17675. go
  17676. dump tran master with no_log
  17677. go
  17678. create procedure sp_indexes_rowset;5
  17679.     (
  17680.     @table_server        sysname,
  17681.     @table_catalog        sysname = null,
  17682.     @table_name        sysname = null,
  17683.     @index_name        sysname = null,
  17684.     @table_schema        sysname = null
  17685.     )
  17686. as
  17687.     select
  17688.         TABLE_CATALOG,    
  17689.         TABLE_SCHEMA,    
  17690.         TABLE_NAME,    
  17691.         INDEX_CATALOG,        
  17692.         INDEX_SCHEMA,    
  17693.         INDEX_NAME,    
  17694.         PRIMARY_KEY,    
  17695.         "UNIQUE",    
  17696.         "CLUSTERED",    
  17697.         "TYPE",        
  17698.         FILL_FACTOR,    
  17699.         INITIAL_SIZE,    
  17700.         NULLS,        
  17701.         SORT_BOOKMARKS,    
  17702.         AUTO_UPDATE,    
  17703.         NULL_COLLATION,    
  17704.         ORDINAL_POSITION,
  17705.         COLUMN_NAME,    
  17706.         COLUMN_GUID,    
  17707.         COLUMN_PROPID,    
  17708.         COLLATION,    
  17709.         CARDINALITY,    
  17710.         PAGES,        
  17711.         FILTER_CONDITION
  17712.     --    INTEGRATED
  17713.     from master.dbo.SYSREMOTE_INDEXES <
  17714.                 @table_server,
  17715.                 @table_catalog,
  17716.                 @table_schema,
  17717.                         @index_name,
  17718.                 NULL,            /* TYPE (index type) */
  17719.                 @table_name >
  17720.     order by 8 desc, 4, 5, 6, 17
  17721. go
  17722.  
  17723. grant execute on sp_indexes_rowset to public
  17724. go
  17725.  
  17726. dump tran master with no_log
  17727. go
  17728. if (charindex('6.00', @@version) > 0)
  17729.     begin
  17730.     if (exists (select * from sysobjects
  17731.             where name = 'sp_indexes_rowset' and type = 'P '))
  17732.         begin
  17733.         drop procedure sp_indexes_rowset
  17734.         dump tran master with no_log
  17735.         end
  17736.     end
  17737. go
  17738.  
  17739.  
  17740. print ''
  17741. print 'creating sp_primary_keys_rowset'
  17742. go
  17743.  
  17744. /*    Procedure for 6.0 and 6.5 servers */
  17745. create procedure sp_primary_keys_rowset
  17746.     (
  17747.        @table_name        varchar(255),
  17748.     @table_schema         varchar(244) = null
  17749.     )
  17750. as
  17751.     select    TABLE_CATALOG    = db_name(),
  17752.             TABLE_SCHEMA    = user_name(o.uid),
  17753.             TABLE_NAME        = o.name,    
  17754.             COLUMN_NAME        = c.name,
  17755.             COLUMN_GUID        = convert(binary(16),null),
  17756.             COLUMN_PROPID    = convert(int,null),
  17757.             ORDINAL            = convert(int,c1.colid),
  17758.             PK_NAME            = i.name
  17759.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17760.     where    o.type in ('U')
  17761.     and     o.name = @table_name
  17762.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17763.     and     o.id = c.id
  17764.     and     o.id = i.id
  17765.     and     (i.status & 0x800) = 0x800
  17766.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17767.     and     c1.id = c.id
  17768.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17769.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17770.     and     (
  17771.         suser_id() = 1     /* User is the System Administrator */
  17772.         or o.uid = user_id()     /* User created the object */
  17773.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17774.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17775.             from sysprotects p
  17776.             where p.id = o.id
  17777.             /* get rows for public,current user,user's group */
  17778.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17779.             /* check for SELECT,EXECUTE privilege */
  17780.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17781.             ) = 1    /* final magic...compare Grants      */
  17782.            )
  17783.     order by 2, 3
  17784. go
  17785. dump tran master with no_log
  17786. go
  17787. create procedure sp_primary_keys_rowset;2
  17788.     (
  17789.      @table_schema         varchar(244) = null
  17790.     )
  17791. as
  17792.     select    TABLE_CATALOG    = db_name(),
  17793.             TABLE_SCHEMA    = user_name(o.uid),
  17794.             TABLE_NAME        = o.name,    
  17795.             COLUMN_NAME        = c.name,
  17796.             COLUMN_GUID        = convert(binary(16),null),
  17797.             COLUMN_PROPID    = convert(int,null),
  17798.             ORDINAL            = convert(int,c1.colid),
  17799.             PK_NAME            = i.name
  17800.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17801.     where    o.type in ('U')
  17802.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17803.     and     o.id = c.id
  17804.     and     o.id = i.id
  17805.     and     (i.status & 0x800) = 0x800
  17806.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17807.     and     c1.id = c.id
  17808.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17809.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17810.     and     (
  17811.         suser_id() = 1     /* User is the System Administrator */
  17812.         or o.uid = user_id()     /* User created the object */
  17813.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17814.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17815.             from sysprotects p
  17816.             where p.id = o.id
  17817.             /* get rows for public,current user,user's group */
  17818.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17819.             /* check for SELECT,EXECUTE privilege */
  17820.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17821.             ) = 1    /* final magic...compare Grants      */
  17822.            )
  17823.     order by 2, 3
  17824. go
  17825.  
  17826. if (charindex('7.00', @@version) = 0)
  17827. begin
  17828.     print ''
  17829.     print ''
  17830.     print 'Warning:'
  17831.     print 'you are installing the stored procedures '
  17832.     print 'on a pre 7.0 SQL Server.'
  17833.     print 'Ignore the following errors.'
  17834. end
  17835. else
  17836.     drop proc sp_primary_keys_rowset
  17837. go
  17838.  
  17839. /*    Procedure for 7.0 servers */
  17840. create procedure sp_primary_keys_rowset
  17841.     (
  17842.        @table_name        sysname,
  17843.     @table_schema         sysname = null
  17844.     )
  17845. as
  17846.     select    TABLE_CATALOG    = db_name(),
  17847.             TABLE_SCHEMA    = user_name(o.uid),
  17848.             TABLE_NAME        = o.name,    
  17849.             COLUMN_NAME        = c.name,
  17850.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17851.             COLUMN_PROPID    = convert(int,null),
  17852.             ORDINAL            = convert(int,c1.colid),
  17853.             PK_NAME            = i.name
  17854.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17855.     where    o.type in ('U')
  17856.     and     o.name = @table_name
  17857.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17858.     and     o.id = c.id
  17859.     and     o.id = i.id
  17860.     and     (i.status & 0x800) = 0x800
  17861.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17862.     and     c1.id = c.id
  17863.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17864.     and    permissions(o.id) <> 0
  17865.     order by 2, 3
  17866. go
  17867. dump tran master with no_log
  17868. go
  17869. create procedure sp_primary_keys_rowset;2
  17870.     (
  17871.      @table_schema     sysname = null
  17872.     )
  17873. as
  17874.     select    TABLE_CATALOG    = db_name(),
  17875.             TABLE_SCHEMA    = user_name(o.uid),
  17876.             TABLE_NAME        = o.name,    
  17877.             COLUMN_NAME        = c.name,
  17878.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17879.             COLUMN_PROPID    = convert(int,null),
  17880.             ORDINAL            = convert(int,c1.colid),
  17881.             PK_NAME            = i.name
  17882.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17883.     where    o.type in ('U')
  17884.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17885.     and     o.id = c.id
  17886.     and     o.id = i.id
  17887.     and     (i.status & 0x800) = 0x800
  17888.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17889.     and     c1.id = c.id
  17890.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17891.     and    permissions(o.id) <> 0
  17892.     order by 2, 3
  17893. go
  17894. dump tran master with no_log
  17895. go
  17896. create procedure sp_primary_keys_rowset;3
  17897.     (
  17898.        @table_name        sysname = null,
  17899.     @table_schema         sysname = null
  17900.     )
  17901. as
  17902. IF @table_name is not NULL
  17903.     BEGIN
  17904.     select    TABLE_CATALOG    = db_name(),
  17905.             TABLE_SCHEMA    = user_name(o.uid),
  17906.             TABLE_NAME        = o.name,    
  17907.             COLUMN_NAME        = c.name,
  17908.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17909.             COLUMN_PROPID    = convert(int,null),
  17910.             ORDINAL            = convert(int,c1.colid),
  17911.             PK_NAME            = i.name
  17912.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17913.     where    o.type in ('U')
  17914.     and     o.name = @table_name
  17915.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17916.     and     o.id = c.id
  17917.     and     o.id = i.id
  17918.     and     (i.status & 0x800) = 0x800
  17919.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17920.     and     c1.id = c.id
  17921.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17922.     and    permissions(o.id) <> 0
  17923.     END
  17924. ELSE
  17925.     BEGIN
  17926.     select    TABLE_CATALOG    = db_name(),
  17927.             TABLE_SCHEMA    = user_name(o.uid),
  17928.             TABLE_NAME        = o.name,    
  17929.             COLUMN_NAME        = c.name,
  17930.             COLUMN_GUID        = convert(uniqueidentifier,null),
  17931.             COLUMN_PROPID    = convert(int,null),
  17932.             ORDINAL            = convert(int,c1.colid),
  17933.             PK_NAME            = i.name
  17934.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17935.     where    o.type in ('U')
  17936.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17937.     and     o.id = c.id
  17938.     and     o.id = i.id
  17939.     and     (i.status & 0x800) = 0x800
  17940.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17941.     and     c1.id = c.id
  17942.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17943.     and    permissions(o.id) <> 0
  17944.     END
  17945. go
  17946. dump tran master with no_log
  17947. go
  17948. create procedure sp_primary_keys_rowset;5
  17949.     (
  17950.     @table_server        sysname,
  17951.     @table_catalog        sysname = null,
  17952.     @table_name            sysname = null,
  17953.     @table_schema        sysname = null
  17954.     )
  17955. as
  17956.     select
  17957.         TABLE_CATALOG,    
  17958.         TABLE_SCHEMA,    
  17959.         TABLE_NAME,    
  17960.         COLUMN_NAME,    
  17961.         COLUMN_GUID,    
  17962.         COLUMN_PROPID,    
  17963.         ORDINAL
  17964.         --PK_NAME        
  17965.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  17966.                 @table_server,
  17967.                 @table_catalog,
  17968.                 @table_schema,
  17969.                 @table_name >
  17970.     order by 1,2,3
  17971. go
  17972.  
  17973. grant execute on sp_primary_keys_rowset to public
  17974. go
  17975.  
  17976. dump tran master with no_log
  17977. go
  17978. if (charindex('6.00', @@version) > 0)
  17979.     begin
  17980.     if (exists (select * from sysobjects
  17981.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  17982.         begin
  17983.         drop procedure sp_primary_keys_rowset
  17984.         dump tran master with no_log
  17985.         end
  17986.     end
  17987. go
  17988.  
  17989.  
  17990. print ''
  17991. print 'creating sp_provider_types_rowset'
  17992. go
  17993.  
  17994. /*    Procedure for 6.0 and 6.50 servers */
  17995. create proc sp_provider_types_rowset
  17996.     (
  17997.     @data_type     smallint = null,
  17998.     @best_match    tinyint  = null
  17999.     )
  18000. as
  18001.     select
  18002.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  18003.                         else d.type_name 
  18004.                         end,
  18005.         DATA_TYPE         = d.oledb_data_type,                            
  18006.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18007.                             then @@max_precision
  18008.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  18009.         LITERAL_PREFIX         = d.literal_prefix,
  18010.         LITERAL_SUFFIX         = d.literal_suffix,
  18011.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  18012.         IS_NULLABLE        = t.allownulls,
  18013.         CASE_SENSITIVE        = d.case_sensitive,
  18014.         SEARCHABLE         = d.searchable,
  18015.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  18016.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  18017.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  18018.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  18019.                         else d.local_type_name
  18020.                         end,
  18021.         MINIMUM_SCALE        = convert(smallint,
  18022.                         case 
  18023.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  18024.                         else null 
  18025.                         end),
  18026.         MAXIMUM_SCALE        = convert(smallint,
  18027.                         case 
  18028.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18029.                         then @@max_precision
  18030.                         else null 
  18031.                         end),
  18032.         GUID            = convert(binary(16),null),
  18033.         TYPELIB            = convert(varchar(1),null),
  18034.         VERSION            = convert(varchar(1),null),
  18035.         IS_LONG            = d.is_long,
  18036.         BEST_MATCH        = case when t.usertype = 80
  18037.                         then convert(bit,0)
  18038.                         else d.best_match
  18039.                         end,
  18040.         IS_FIXEDLENGTH        = convert(bit, 
  18041.                         case when d.fixlen is null then 0 else 1 end)
  18042.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  18043.     where    d.ss_dtype = t.type 
  18044.     and    t.usertype <= 100
  18045.     and    t.usertype <> 18 /* sysname */
  18046.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  18047.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  18048.     and     t.usertype *= e.user_type 
  18049.     and     e.AUTO_INCREMENT = 0
  18050.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  18051.     and    (@data_type is null or d.oledb_data_type = @data_type)
  18052.     and    (@best_match is null or d.best_match = @best_match)
  18053.     order by 2
  18054. go
  18055. dump tran master with no_log
  18056. go
  18057.  
  18058. if (charindex('7.00', @@version) = 0)
  18059. begin
  18060.     print ''
  18061.     print ''
  18062.     print 'Warning:'
  18063.     print 'you are installing the stored procedures '
  18064.     print 'on a pre 7.0 SQL Server.'
  18065.     print 'Ignore the following errors.'
  18066. end
  18067. else
  18068.     drop proc sp_provider_types_rowset
  18069. go
  18070.  
  18071. /*    Procedure for 7.0 server */
  18072. create proc sp_provider_types_rowset
  18073.     (
  18074.     @data_type     smallint = null,
  18075.     @best_match    tinyint  = null
  18076.     )
  18077. as
  18078.     select
  18079.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  18080.                         else d.type_name 
  18081.                         end,
  18082.         DATA_TYPE         = d.oledb_data_type,                            
  18083.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18084.                             then @@max_precision
  18085.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  18086.         LITERAL_PREFIX         = d.literal_prefix,
  18087.         LITERAL_SUFFIX         = d.literal_suffix,
  18088.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  18089.         IS_NULLABLE        = t.allownulls,
  18090.         CASE_SENSITIVE        = d.case_sensitive,
  18091.         SEARCHABLE         = d.searchable,
  18092.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  18093.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  18094.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  18095.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  18096.                         else d.local_type_name
  18097.                         end,
  18098.         MINIMUM_SCALE        = convert(smallint,
  18099.                         case 
  18100.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  18101.                         else null 
  18102.                         end),
  18103.         MAXIMUM_SCALE        = convert(smallint,
  18104.                         case 
  18105.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  18106.                         then @@max_precision
  18107.                         else null 
  18108.                         end),
  18109.         GUID            = convert(uniqueidentifier,null),
  18110.         TYPELIB            = convert(nvarchar(1),null),
  18111.         VERSION            = convert(nvarchar(1),null),
  18112.         IS_LONG            = d.is_long,
  18113.         BEST_MATCH        = case    when t.usertype = 80
  18114.                         then convert(bit,0)
  18115.                         else d.best_match
  18116.                         end,
  18117.         IS_FIXEDLENGTH        = convert(bit, 
  18118.                         case when d.fixlen is null then 0 else 1 end)
  18119.  
  18120.     from master.dbo.spt_provider_types d
  18121.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  18122.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  18123.             t.xusertype = e.user_type
  18124.             and e.AUTO_INCREMENT = 0
  18125.     where    (@data_type is null or d.oledb_data_type = @data_type)
  18126.     and    (@best_match is null or d.best_match = @best_match)    
  18127.     and    t.usertype <= 255
  18128.     and    t.usertype <> 18 /* sysname */
  18129.     order by 2
  18130. go
  18131.  
  18132. grant execute on sp_provider_types_rowset to public
  18133. go
  18134.  
  18135. dump tran master with no_log
  18136. go
  18137. if (charindex('6.00', @@version) > 0)
  18138.     begin
  18139.     if (exists (select * from sysobjects
  18140.         where name = 'sp_provider_types_rowset' and type = 'P '))
  18141.         begin
  18142.         drop procedure sp_provider_types_rowset
  18143.         dump tran master with no_log
  18144.         end
  18145.     end
  18146. go
  18147.  
  18148.  
  18149. print ''
  18150. print 'creating sp_procedure_params_rowset'
  18151. go
  18152.  
  18153. /*    Procedure for 6.0 and 6.50 servers */
  18154. create procedure sp_procedure_params_rowset
  18155.     (
  18156.        @procedure_name        varchar(255) = null,
  18157.     @group_number        int = null,
  18158.     @procedure_schema     varchar(255) = null,
  18159.     @parameter_name        varchar(255) = null
  18160.     )
  18161. as
  18162. IF @procedure_name is not null
  18163.     BEGIN
  18164.     select
  18165.         PROCEDURE_CATALOG     = db_name(),
  18166.         PROCEDURE_SCHEMA     = user_name(),
  18167.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18168.         PARAMETER_NAME         = c.name,
  18169.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18170.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18171.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18172.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18173.         IS_NULLABLE        = convert(bit,
  18174.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18175.                         then 0 else 1
  18176.                         end),
  18177.         DATA_TYPE        = d.oledb_data_type,
  18178.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18179.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18180.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18181.                         then coalesce(d.column_size,c.length)
  18182.                         else null 
  18183.                         end),
  18184.         CHARACTER_OCTET_LENGTH    = convert(int,
  18185.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18186.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18187.                         then coalesce(d.column_size,c.length)
  18188.                         else null 
  18189.                         end),
  18190.         NUMERIC_PRECISION    = convert(smallint,
  18191.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  18192.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18193.         NUMERIC_SCALE        = convert(smallint, 
  18194.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18195.         DESCRIPTION        = convert(varchar(1),null),
  18196.         TYPE_NAME        = d.type_name,
  18197.         LOCAL_TYPE_NAME        = d.local_type_name
  18198.     
  18199.     from
  18200.         syscolumns c,
  18201.         sysobjects o,
  18202.         master.dbo.spt_provider_types d,
  18203.         systypes t
  18204.     where
  18205.         o.name = @procedure_name
  18206.     and    o.type = 'P'                            /* Just Procedures */
  18207.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18208.     and    o.id = c.id
  18209.     and    c.number = @group_number
  18210.     and    c.type = d.ss_dtype
  18211.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18212.     and    c.usertype = t.usertype
  18213.     and    (t.usertype != 80 or d.type_name='timestamp')
  18214.     and    (@parameter_name is null or @parameter_name = c.name)
  18215.     UNION ALL
  18216.     SELECT           /* return value row*/
  18217.         PROCEDURE_CATALOG     = db_name(),
  18218.         PROCEDURE_SCHEMA     = user_name(),
  18219.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18220.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18221.         ORDINAL_POSITION     = convert(smallint,0),
  18222.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18223.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18224.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18225.         IS_NULLABLE        = convert(bit,0),
  18226.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18227.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18228.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18229.         NUMERIC_PRECISION    = convert(smallint,10),
  18230.         NUMERIC_SCALE        = convert(smallint,null),
  18231.         DESCRIPTION        = convert(varchar(1),null),
  18232.         TYPE_NAME        = convert(sysname,'int'),
  18233.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18234.     from
  18235.         syscomments c,
  18236.         sysobjects o
  18237.     where
  18238.         o.name = @procedure_name
  18239.     and    o.type = 'P'                        /* Just Procedures */
  18240.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18241.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18242.     and    c.id = o.id
  18243.     and    c.number = @group_number
  18244.     and c.colid = 1
  18245.     order by 2, 3, 5
  18246.     END
  18247. ELSE
  18248.     BEGIN
  18249.     select
  18250.         PROCEDURE_CATALOG     = db_name(),
  18251.         PROCEDURE_SCHEMA     = user_name(),
  18252.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18253.         PARAMETER_NAME         = c.name,
  18254.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18255.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18256.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18257.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18258.         IS_NULLABLE        = convert(bit,
  18259.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18260.                         then 0 else 1
  18261.                         end),
  18262.         DATA_TYPE        = d.oledb_data_type,
  18263.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18264.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18265.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18266.                         then coalesce(d.column_size,c.length)
  18267.                         else null 
  18268.                         end),
  18269.         CHARACTER_OCTET_LENGTH    = convert(int,
  18270.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18271.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18272.                         then coalesce(d.column_size,c.length)
  18273.                         else null 
  18274.                         end),
  18275.         NUMERIC_PRECISION    = convert(smallint,
  18276.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18277.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18278.         NUMERIC_SCALE        = convert(smallint, 
  18279.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18280.         DESCRIPTION        = convert(varchar(1),null),
  18281.         TYPE_NAME        = d.type_name,
  18282.         LOCAL_TYPE_NAME        = d.local_type_name
  18283.     
  18284.     from
  18285.         syscolumns c,
  18286.         sysobjects o,
  18287.         master.dbo.spt_provider_types d,
  18288.         systypes t
  18289.     where
  18290.         o.type = 'P'                            /* Just Procedures */
  18291.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18292.     and    o.id = c.id
  18293.     and    c.type = d.ss_dtype
  18294.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18295.     and    c.usertype = t.usertype
  18296.     and    (t.usertype != 80 or d.type_name='timestamp')
  18297.     and    (@parameter_name is null or @parameter_name = c.name)
  18298.     UNION ALL
  18299.     SELECT           /* return value row*/
  18300.         PROCEDURE_CATALOG     = db_name(),
  18301.         PROCEDURE_SCHEMA     = user_name(),
  18302.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18303.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18304.         ORDINAL_POSITION     = convert(smallint,0),
  18305.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18306.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18307.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18308.         IS_NULLABLE        = convert(bit,0),
  18309.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18310.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18311.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18312.         NUMERIC_PRECISION    = convert(smallint,10),
  18313.         NUMERIC_SCALE        = convert(smallint,null),
  18314.         DESCRIPTION        = convert(varchar(1),null),
  18315.         TYPE_NAME        = convert(sysname,'int'),
  18316.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18317.     from
  18318.         syscomments c,
  18319.         sysobjects o
  18320.     where
  18321.         o.type = 'P'                        /* Just Procedures */
  18322.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18323.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18324.     and    c.id = o.id
  18325.     and     c.colid = 1
  18326.     order by 2, 3, 5
  18327.     END
  18328. go
  18329. dump tran master with no_log
  18330. go
  18331. create procedure sp_procedure_params_rowset;2
  18332.     (
  18333.       @handle            int output,
  18334.        @scrollopt        int output,
  18335.     @ccopt            int output,
  18336.     @rows            int output,
  18337.        @procedure_name        varchar(255) = null,
  18338.     @group_number        int = null,
  18339.     @procedure_schema     varchar(255) = null,
  18340.     @parameter_name        varchar(255) = null
  18341.     )
  18342. as
  18343.     declare @ret int
  18344.  
  18345.  
  18346. SET NOCOUNT ON
  18347.  
  18348.     create table #spprocparamrowset1
  18349.         (
  18350.         PROCEDURE_CATALOG     sysname not null,
  18351.         PROCEDURE_SCHEMA     sysname not null,
  18352.         PROCEDURE_NAME         varchar(35) not null,
  18353.         PARAMETER_NAME         sysname not null,
  18354.         ORDINAL_POSITION     smallint not null,
  18355.         PARAMETER_TYPE         smallint null,
  18356.         PARAMETER_HASDEFAULT    tinyint null,
  18357.         PARAMETER_DEFAULT    varchar(255) null,
  18358.         IS_NULLABLE        bit not null,
  18359.         DATA_TYPE        smallint null,
  18360.         CHARACTER_MAXIMUM_LENGTH int null,
  18361.         CHARACTER_OCTET_LENGTH    int null,
  18362.         NUMERIC_PRECISION    smallint null,
  18363.         NUMERIC_SCALE        smallint null,
  18364.         DESCRIPTION        varchar(1) null,
  18365.         TYPE_NAME        sysname null,
  18366.         LOCAL_TYPE_NAME        sysname null,
  18367.         )
  18368.  
  18369. IF @procedure_name is not null
  18370.     BEGIN
  18371.     insert into #spprocparamrowset1
  18372.     select
  18373.         PROCEDURE_CATALOG     = db_name(),
  18374.         PROCEDURE_SCHEMA     = user_name(),
  18375.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18376.         PARAMETER_NAME         = c.name,
  18377.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18378.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18379.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18380.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18381.         IS_NULLABLE        = convert(bit,
  18382.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18383.                         then 0 else 1
  18384.                         end),
  18385.         DATA_TYPE        = d.oledb_data_type,
  18386.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18387.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18388.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18389.                         then coalesce(d.column_size,c.length)
  18390.                         else null 
  18391.                         end),
  18392.         CHARACTER_OCTET_LENGTH    = convert(int,
  18393.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18394.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18395.                         then coalesce(d.column_size,c.length)
  18396.                         else null 
  18397.                         end),
  18398.         NUMERIC_PRECISION    = convert(smallint,
  18399.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18400.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18401.         NUMERIC_SCALE        = convert(smallint, 
  18402.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18403.         DESCRIPTION        = convert(varchar(1),null),
  18404.         TYPE_NAME        = d.type_name,
  18405.         LOCAL_TYPE_NAME        = d.local_type_name
  18406.     
  18407.     from
  18408.         syscolumns c,
  18409.         sysobjects o,
  18410.         master.dbo.spt_provider_types d,
  18411.         systypes t
  18412.     where
  18413.         o.name = @procedure_name
  18414.     and    o.type = 'P'                            /* Just Procedures */
  18415.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18416.     and    o.id = c.id
  18417.     and    c.number = @group_number
  18418.     and    c.type = d.ss_dtype
  18419.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18420.     and    c.usertype = t.usertype
  18421.     and    (t.usertype != 80 or d.type_name='timestamp')
  18422.     and    (@parameter_name is null or @parameter_name = c.name)
  18423.     UNION ALL
  18424.     SELECT           /* return value row*/
  18425.         PROCEDURE_CATALOG     = db_name(),
  18426.         PROCEDURE_SCHEMA     = user_name(),
  18427.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18428.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18429.         ORDINAL_POSITION     = convert(smallint,0),
  18430.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18431.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18432.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18433.         IS_NULLABLE        = convert(bit,0),
  18434.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18435.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18436.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18437.         NUMERIC_PRECISION    = convert(smallint,10),
  18438.         NUMERIC_SCALE        = convert(smallint,null),
  18439.         DESCRIPTION        = convert(varchar(1),null),
  18440.         TYPE_NAME        = convert(sysname,'int'),
  18441.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18442.     from
  18443.         syscomments c,
  18444.         sysobjects o
  18445.     where
  18446.         o.name = @procedure_name
  18447.     and    o.type = 'P'                        /* Just Procedures */
  18448.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18449.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18450.     and    c.id = o.id
  18451.     and    c.number = @group_number
  18452.     and c.colid = 1
  18453.     order by 2, 3, 5
  18454.     END
  18455. ELSE
  18456.     BEGIN
  18457.     insert into #spprocparamrowset1
  18458.     select
  18459.         PROCEDURE_CATALOG     = db_name(),
  18460.         PROCEDURE_SCHEMA     = user_name(),
  18461.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18462.         PARAMETER_NAME         = c.name,
  18463.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18464.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  18465.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18466.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18467.         IS_NULLABLE        = convert(bit,
  18468.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  18469.                         then 0 else 1
  18470.                         end),
  18471.         DATA_TYPE        = d.oledb_data_type,
  18472.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18473.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18474.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18475.                         then coalesce(d.column_size,c.length)
  18476.                         else null 
  18477.                         end),
  18478.         CHARACTER_OCTET_LENGTH    = convert(int,
  18479.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18480.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18481.                         then coalesce(d.column_size,c.length)
  18482.                         else null 
  18483.                         end),
  18484.         NUMERIC_PRECISION    = convert(smallint,
  18485.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18486.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18487.         NUMERIC_SCALE        = convert(smallint, 
  18488.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18489.         DESCRIPTION        = convert(varchar(1),null),
  18490.         TYPE_NAME        = d.type_name,
  18491.         LOCAL_TYPE_NAME        = d.local_type_name
  18492.     
  18493.     from
  18494.         syscolumns c,
  18495.         sysobjects o,
  18496.         master.dbo.spt_provider_types d,
  18497.         systypes t
  18498.     where
  18499.         o.type = 'P'                            /* Just Procedures */
  18500.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18501.     and    o.id = c.id
  18502.     and    c.type = d.ss_dtype
  18503.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18504.     and    c.usertype = t.usertype
  18505.     and    (t.usertype != 80 or d.type_name='timestamp')
  18506.     and    (@parameter_name is null or @parameter_name = c.name)
  18507.     UNION ALL
  18508.     SELECT           /* return value row*/
  18509.         PROCEDURE_CATALOG     = db_name(),
  18510.         PROCEDURE_SCHEMA     = user_name(),
  18511.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18512.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18513.         ORDINAL_POSITION     = convert(smallint,0),
  18514.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18515.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  18516.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18517.         IS_NULLABLE        = convert(bit,0),
  18518.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18519.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18520.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18521.         NUMERIC_PRECISION    = convert(smallint,10),
  18522.         NUMERIC_SCALE        = convert(smallint,null),
  18523.         DESCRIPTION        = convert(varchar(1),null),
  18524.         TYPE_NAME        = convert(sysname,'int'),
  18525.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  18526.     from
  18527.         syscomments c,
  18528.         sysobjects o
  18529.     where
  18530.         o.type = 'P'                        /* Just Procedures */
  18531.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18532.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18533.     and    c.id = o.id
  18534.     and     c.colid = 1
  18535.     order by 2, 3, 5
  18536.     END
  18537.  
  18538.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  18539.         @scrollopt output, @ccopt output, @rows output
  18540.  
  18541.     drop table #spprocparamrowset1
  18542.        return isnull(@ret,0)
  18543. go
  18544. dump tran master with no_log
  18545. go
  18546. create procedure sp_procedure_params_rowset;3
  18547. as
  18548.     select
  18549.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  18550.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  18551.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  18552.         PARAMETER_NAME         = convert(sysname, ' '),
  18553.         ORDINAL_POSITION     = convert(smallint, 0),
  18554.         PARAMETER_TYPE         = convert(smallint, 0),
  18555.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18556.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18557.         IS_NULLABLE        = convert(bit, 0),
  18558.         DATA_TYPE        = convert(smallint, 0),
  18559.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  18560.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  18561.         NUMERIC_PRECISION    = convert(smallint, 0),
  18562.         NUMERIC_SCALE        = convert(smallint, 0),
  18563.         DESCRIPTION        = convert(varchar(1),null),
  18564.         TYPE_NAME        = convert(sysname,null),
  18565.         LOCAL_TYPE_NAME        = convert(sysname,null)
  18566.     where    1=0
  18567. go
  18568. dump tran master with no_log
  18569. go
  18570.  
  18571.  
  18572. if (charindex('7.00', @@version) > 0)
  18573.     drop procedure sp_procedure_params_rowset
  18574. else
  18575. begin
  18576.     print ''
  18577.     print ''
  18578.     print 'Warning:'
  18579.     print 'you are installing the stored procedures '
  18580.     print 'on a pre 7.0 SQL Server.'
  18581.     print 'Ignore the following errors.'
  18582. end
  18583. go
  18584.  
  18585. /*    Procedure for 7.0 servers */
  18586. create procedure sp_procedure_params_rowset
  18587.     (
  18588.        @procedure_name        sysname,
  18589.     @group_number        int = 1,
  18590.     @procedure_schema     sysname = null,
  18591.     @parameter_name        sysname = null
  18592.     )
  18593. as
  18594.     select
  18595.         PROCEDURE_CATALOG     = db_name(),
  18596.         PROCEDURE_SCHEMA     = user_name(),
  18597.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18598.         PARAMETER_NAME         = c.name,
  18599.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18600.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  18601.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18602.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18603.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  18604.         DATA_TYPE        = d.oledb_data_type,
  18605.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18606.                         case 
  18607.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18608.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18609.                         then coalesce(d.column_size,c.length)
  18610.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18611.                         then coalesce(d.column_size,c.length/2)
  18612.                         else null 
  18613.                         end),
  18614.         CHARACTER_OCTET_LENGTH    = convert(int,
  18615.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18616.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18617.                         then coalesce(d.column_size,c.length)
  18618.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18619.                         then coalesce(d.column_size*2,c.length)
  18620.                         else null 
  18621.                         end),
  18622.         NUMERIC_PRECISION    = convert(smallint,
  18623.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18624.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18625.         NUMERIC_SCALE        = convert(smallint, 
  18626.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18627.         DESCRIPTION        = convert(nvarchar(1),null),
  18628.         TYPE_NAME        = d.type_name,
  18629.         LOCAL_TYPE_NAME        = d.local_type_name
  18630.     
  18631.     from
  18632.         sysobjects o,
  18633.         syscolumns c,
  18634.         master.dbo.spt_provider_types d,
  18635.         systypes t
  18636.     where
  18637.         o.name = @procedure_name
  18638.     and    o.type = 'P'                            /* Just Procedures */
  18639.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18640.     and    o.id = c.id
  18641.     and    c.number = @group_number
  18642.     and    c.xtype = d.ss_dtype
  18643.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18644.     and    c.xusertype = t.xusertype
  18645.     and    (@parameter_name is null or @parameter_name = c.name)
  18646.     UNION ALL
  18647.     SELECT           /* return value row*/
  18648.         PROCEDURE_CATALOG     = db_name(),
  18649.         PROCEDURE_SCHEMA     = user_name(),
  18650.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18651.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18652.         ORDINAL_POSITION     = convert(smallint,0),
  18653.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18654.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18655.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18656.         IS_NULLABLE        = convert(bit,0),
  18657.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18658.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18659.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18660.         NUMERIC_PRECISION    = convert(smallint,10),
  18661.         NUMERIC_SCALE        = convert(smallint,null),
  18662.         DESCRIPTION        = convert(nvarchar(1),null),
  18663.         TYPE_NAME        = convert(sysname,N'int'),
  18664.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  18665.     from
  18666.         sysobjects o,
  18667.         syscomments c
  18668.     where
  18669.         o.name = @procedure_name
  18670.     and    o.id = c.id 
  18671.     and    c.number = @group_number
  18672.     and    c.colid = 1
  18673.     and    o.type = 'P'                        /* Just Procedures */
  18674.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18675.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18676.     order by 2, 3, 5
  18677. go
  18678. dump tran master with no_log
  18679. go
  18680. create procedure sp_procedure_params_rowset;2
  18681.     (
  18682.     @procedure_schema     sysname = null,
  18683.     @parameter_name        sysname = null
  18684.     )
  18685. as
  18686.     select
  18687.         PROCEDURE_CATALOG     = db_name(),
  18688.         PROCEDURE_SCHEMA     = user_name(),
  18689.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18690.         PARAMETER_NAME         = c.name,
  18691.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18692.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  18693.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18694.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18695.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  18696.         DATA_TYPE        = d.oledb_data_type,
  18697.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18698.                         case 
  18699.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18700.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18701.                         then coalesce(d.column_size,c.length)
  18702.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18703.                         then coalesce(d.column_size,c.length/2)
  18704.                         else null 
  18705.                         end),
  18706.         CHARACTER_OCTET_LENGTH    = convert(int,
  18707.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18708.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18709.                         then coalesce(d.column_size,c.length)
  18710.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18711.                         then coalesce(d.column_size*2,c.length)
  18712.                         else null 
  18713.                         end),
  18714.         NUMERIC_PRECISION    = convert(smallint,
  18715.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  18716.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  18717.         NUMERIC_SCALE        = convert(smallint, 
  18718.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18719.         DESCRIPTION        = convert(nvarchar(1),null),
  18720.         TYPE_NAME        = d.type_name,
  18721.         LOCAL_TYPE_NAME        = d.local_type_name
  18722.     
  18723.     from
  18724.         sysobjects o,
  18725.         syscolumns c,
  18726.         master.dbo.spt_provider_types d,
  18727.         systypes t
  18728.     where
  18729.         o.type = 'P'                            /* Just Procedures */
  18730.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18731.     and    o.id = c.id
  18732.     and    c.xtype = d.ss_dtype
  18733.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18734.     and    c.xusertype = t.xusertype
  18735.     and    (@parameter_name is null or @parameter_name = c.name)
  18736.     UNION ALL
  18737.     SELECT           /* return value row*/
  18738.         PROCEDURE_CATALOG     = db_name(),
  18739.         PROCEDURE_SCHEMA     = user_name(),
  18740.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  18741.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  18742.         ORDINAL_POSITION     = convert(smallint,0),
  18743.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18744.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  18745.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  18746.         IS_NULLABLE        = convert(bit,0),
  18747.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18748.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18749.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18750.         NUMERIC_PRECISION    = convert(smallint,10),
  18751.         NUMERIC_SCALE        = convert(smallint,null),
  18752.         DESCRIPTION        = convert(nvarchar(1),null),
  18753.         TYPE_NAME        = convert(sysname,N'int'),
  18754.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  18755.     from
  18756.         sysobjects o,
  18757.         syscomments c
  18758.     where
  18759.         o.type = 'P'        /* Just Procedures */
  18760.     and    o.id = c.id 
  18761.     and    c.colid = 1
  18762.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18763.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18764.     order by 2, 3, 5
  18765. go
  18766.  
  18767.  
  18768. grant execute on sp_procedure_params_rowset to public
  18769. go
  18770.  
  18771. dump tran master with no_log
  18772. go
  18773. if (charindex('6.00', @@version) > 0)
  18774.     begin
  18775.     if (exists (select * from sysobjects
  18776.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  18777.         begin
  18778.         drop procedure sp_procedure_params_rowset
  18779.         dump tran master with no_log
  18780.         end
  18781.     end
  18782. go
  18783.  
  18784.  
  18785. print ''
  18786. print 'creating sp_procedures_rowset'
  18787. go
  18788.  
  18789.  
  18790. /* pre 7.00 version */
  18791. create procedure sp_procedures_rowset
  18792.     (
  18793.     @procedure_name        varchar(255), 
  18794.     @group_number        int = 1,
  18795.     @procedure_schema    varchar(255) = null
  18796.     )        
  18797. as
  18798.     select
  18799.         PROCEDURE_CATALOG    = db_name(),
  18800.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18801.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18802.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18803.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18804.         DESCRIPTION        = convert(varchar(1),null),
  18805.         DATE_CREATED        = o.crdate,
  18806.         DATE_MODIFIED        = convert(datetime,null)
  18807.     from     
  18808.         sysobjects o, 
  18809.         syscomments p,
  18810.         sysusers u
  18811.     where
  18812.             o.name = @procedure_name
  18813.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18814.     and     o.type = 'P'        /* Object type of Procedure */
  18815.     and     p.colid = 1
  18816.     and     p.id = o.id
  18817.     and    p.number = @group_number
  18818.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18819.     and     (    suser_id() = 1     /* User is the System Administrator */
  18820.         or     o.uid = user_id()    /* User created the object */
  18821.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18822.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18823.              from sysprotects p
  18824.              where p.id = o.id
  18825.                  /*  get rows for public,current user,user's group */
  18826.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18827.                  /* check for SELECT,EXECUTE privilege */
  18828.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18829.             ) = 1     /* final magic...compare Grants    */
  18830.         )
  18831.     order by 2, 3
  18832. go
  18833. dump tran master with no_log
  18834. go
  18835. create procedure sp_procedures_rowset;2
  18836.     (
  18837.     @procedure_schema    varchar(255) = null
  18838.     )        
  18839. as
  18840.     select
  18841.         PROCEDURE_CATALOG    = db_name(),
  18842.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18843.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18844.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18845.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18846.         DESCRIPTION        = convert(varchar(1),null),
  18847.         DATE_CREATED        = o.crdate,
  18848.         DATE_MODIFIED        = convert(datetime,null)
  18849.     from     
  18850.         sysobjects o, 
  18851.         syscomments p,
  18852.         sysusers u
  18853.     where
  18854.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18855.     and     o.type = 'P'        /* Object type of Procedure */
  18856.     and     p.colid = 1
  18857.     and     p.id = o.id
  18858.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18859.     and     (    suser_id() = 1     /* User is the System Administrator */
  18860.         or     o.uid = user_id()    /* User created the object */
  18861.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18862.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18863.              from sysprotects p
  18864.              where p.id = o.id
  18865.                  /*  get rows for public,current user,user's group */
  18866.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18867.                  /* check for SELECT,EXECUTE privilege */
  18868.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18869.             ) = 1     /* final magic...compare Grants    */
  18870.         )
  18871.     order by 2, 3
  18872. go
  18873. dump tran master with no_log
  18874. go
  18875.  
  18876. if (charindex('7.00', @@version) > 0)
  18877.     drop procedure sp_procedures_rowset
  18878. else
  18879. begin
  18880.     print ''
  18881.     print ''
  18882.     print 'Warning:'
  18883.     print 'you are installing the stored procedures '
  18884.     print 'on a pre 7.0 SQL Server.'
  18885.     print 'Ignore the following errors.'
  18886. end
  18887. go
  18888.  
  18889. /* 7.00 version */
  18890. create procedure sp_procedures_rowset
  18891.     (
  18892.     @procedure_name        sysname, 
  18893.     @group_number        int = 1,
  18894.     @procedure_schema    sysname = null
  18895.     )        
  18896. as
  18897.     select
  18898.         PROCEDURE_CATALOG    = db_name(),
  18899.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18900.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  18901.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18902.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  18903.         DESCRIPTION        = convert(nvarchar(1),null),
  18904.         DATE_CREATED        = o.crdate,
  18905.         DATE_MODIFIED        = convert(datetime,null)
  18906.     from     
  18907.         sysobjects o, 
  18908.         syscomments p
  18909.     where
  18910.         permissions(o.id) <> 0
  18911.     and    o.name = @procedure_name
  18912.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18913.     and     o.type = 'P'        /* Object type of Procedure */
  18914.     and     p.colid = 1
  18915.     and     p.id = o.id
  18916.     and    p.number = @group_number
  18917.     order by 2, 3
  18918. go
  18919. dump tran master with no_log
  18920. go
  18921. create procedure sp_procedures_rowset;2
  18922.     (
  18923.     @procedure_schema    sysname = null
  18924.     )        
  18925. as
  18926.     select
  18927.         PROCEDURE_CATALOG    = db_name(),
  18928.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18929.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  18930.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18931.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  18932.         DESCRIPTION        = convert(nvarchar(1),null),
  18933.         DATE_CREATED        = o.crdate,
  18934.         DATE_MODIFIED        = convert(datetime,null)
  18935.     from     sysobjects o, syscomments p
  18936.     where
  18937.         permissions(o.id) <> 0
  18938.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18939.     and     o.type = 'P'        /* Object type of Procedure */
  18940.     and p.colid = 1
  18941.     and p.id = o.id
  18942.     order by 2, 3
  18943. go
  18944.  
  18945. grant execute on sp_procedures_rowset to public
  18946. go
  18947.  
  18948. dump tran master with no_log
  18949. go
  18950. if (charindex('6.00', @@version) > 0)
  18951.     begin
  18952.     if (exists (select * from sysobjects
  18953.             where name = 'sp_procedures_rowset' and type = 'P '))
  18954.         begin
  18955.         drop procedure sp_procedures_rowset
  18956.         dump tran master with no_log
  18957.         end
  18958.     end
  18959. go
  18960.  
  18961.  
  18962. print ''
  18963. print 'creating sp_schemata_rowset'
  18964. go
  18965.  
  18966. /*    6.0 and 6.5 version */
  18967. create procedure sp_schemata_rowset
  18968.     (
  18969.     @schema_name    varchar(90) = null, 
  18970.     @schema_owner    varchar(90) = null
  18971.     )        
  18972. as
  18973.     select    distinct
  18974.         CATALOG_NAME            = db_name(),
  18975.         SCHEMA_NAME            = user_name(o.uid),    
  18976.         SCHEMA_OWNER            = user_name(o.uid),    
  18977.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  18978.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  18979.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  18980.     from    
  18981.         sysobjects o,
  18982.         master.dbo.sysconfigures    cfg,
  18983.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  18984.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  18985.     where   
  18986.         (@schema_name is null or @schema_name = user_name(o.uid))
  18987.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18988.     and    a_cha.type = 1001 /* type is charset */
  18989.     and     b_cha.type = 2001 /* type is sortorder */
  18990.     and     a_cha.id = b_cha.csid
  18991.     and     b_cha.id = cfg.value
  18992.     order by 2
  18993. go
  18994. dump tran master with no_log
  18995. go
  18996.  
  18997. if (charindex('7.00', @@version) > 0)
  18998.     drop procedure sp_schemata_rowset
  18999. else
  19000. begin
  19001.     print ''
  19002.     print ''
  19003.     print 'Warning:'
  19004.     print 'you are installing the stored procedures '
  19005.     print 'on a pre 7.0 SQL Server.'
  19006.     print 'Ignore the following errors.'
  19007. end
  19008. go
  19009.  
  19010. /* 7.00 version */
  19011. create procedure sp_schemata_rowset
  19012.     (
  19013.     @schema_name    sysname = null, 
  19014.     @schema_owner    sysname = null
  19015.     )        
  19016. as
  19017.     select    distinct
  19018.         CATALOG_NAME            = db_name(),
  19019.         SCHEMA_NAME            = user_name(o.uid),    
  19020.         SCHEMA_OWNER            = user_name(o.uid),    
  19021.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  19022.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  19023.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  19024.     from    
  19025.         sysobjects o,
  19026.         master.dbo.sysconfigures    cfg,
  19027.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  19028.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  19029.     where   
  19030.         (@schema_name is null or @schema_name = user_name(o.uid))
  19031.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  19032.     and    a_cha.type = 1001 /* type is charset */
  19033.     and     b_cha.type = 2001 /* type is sortorder */
  19034.     and     a_cha.id = b_cha.csid
  19035.     and     b_cha.id = cfg.value
  19036.     order by 2
  19037. go
  19038. dump tran master with no_log
  19039. go
  19040. go
  19041. /* The following stored procedure is used for Sphinx and Hydra */
  19042. create procedure sp_schemata_rowset;3
  19043. as
  19044.     select
  19045.         CATALOG_NAME            = convert(sysname,' '),
  19046.         SCHEMA_NAME            = convert(sysname,' '),    
  19047.         SCHEMA_OWNER            = convert(sysname,' '),    
  19048.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  19049.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  19050.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  19051.     where    1=0
  19052. go
  19053. grant execute on sp_schemata_rowset to public
  19054. go
  19055.  
  19056. dump tran master with no_log
  19057. go
  19058. if (charindex('6.00', @@version) > 0)
  19059.     begin
  19060.     if (exists (select * from sysobjects
  19061.             where name = 'sp_schemata_rowset' and type = 'P '))
  19062.         begin
  19063.         drop procedure sp_schemata_rowset
  19064.         dump tran master with no_log
  19065.         end
  19066.     end
  19067. go
  19068.  
  19069.  
  19070. print ''
  19071. print 'creating sp_statistics_rowset'
  19072. go
  19073.  
  19074.  
  19075. /*    6.0 and 6.5 version */
  19076. create procedure sp_statistics_rowset
  19077.     (
  19078.     @table_name    varchar(255),
  19079.     @table_schema    varchar(255) = null     
  19080.     )
  19081. as
  19082.     select    db_name()                as TABLE_CATALOG,        
  19083.         user_name(o.uid)            as TABLE_SCHEMA,
  19084.         o.name                    as TABLE_NAME,
  19085.         x.rows                    as CARDINALITY
  19086.     from    sysobjects o, sysindexes x, sysusers u
  19087.     where    o.type in ('U')
  19088.     and     o.name = @table_name
  19089.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19090.     and     x.id = o.id
  19091.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19092.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19093.     and     (
  19094.         suser_id() = 1     /* User is the System Administrator */
  19095.         or o.uid = user_id()     /* User created the object */
  19096.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19097.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19098.             from sysprotects p
  19099.             where p.id = o.id
  19100.             /* get rows for public,current user,user's group */
  19101.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19102.             /* check for SELECT,EXECUTE privilege */
  19103.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19104.             ) = 1    /* final magic...compare Grants      */
  19105.            )
  19106.     order by 2, 3
  19107. go
  19108. dump tran master with no_log
  19109. go
  19110. create procedure sp_statistics_rowset;2
  19111.     (
  19112.     @table_schema    varchar(255) = null     
  19113.     )
  19114. as
  19115.     select    db_name()                as TABLE_CATALOG,        
  19116.         user_name(o.uid)            as TABLE_SCHEMA,
  19117.         o.name                    as TABLE_NAME,
  19118.         x.rows                    as CARDINALITY
  19119.     from    sysobjects o, sysindexes x, sysusers u
  19120.     where    o.type in ('U')
  19121.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19122.     and     x.id = o.id
  19123.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19124.     and     (
  19125.         suser_id() = 1     /* User is the System Administrator */
  19126.         or o.uid = user_id()     /* User created the object */
  19127.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19128.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19129.             from sysprotects p
  19130.             where p.id = o.id
  19131.             /* get rows for public,current user,user's group */
  19132.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19133.             /* check for SELECT,EXECUTE privilege */
  19134.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19135.             ) = 1    /* final magic...compare Grants      */
  19136.            )        
  19137.     order by 2, 3
  19138. go
  19139. dump tran master with no_log
  19140. go
  19141.  
  19142. if (charindex('7.00', @@version) > 0)
  19143.     drop procedure sp_statistics_rowset
  19144. else
  19145. begin
  19146.     print ''
  19147.     print ''
  19148.     print 'Warning:'
  19149.     print 'you are installing the stored procedures '
  19150.     print 'on a pre 7.0 SQL Server.'
  19151.     print 'Ignore the following errors.'
  19152. end
  19153. go
  19154.  
  19155. /*     7.0 version */
  19156. create procedure sp_statistics_rowset
  19157.     (
  19158.     @table_name    sysname,
  19159.     @table_schema    sysname = null     
  19160.     )
  19161. as
  19162.     select    db_name()                as TABLE_CATALOG,        
  19163.         user_name(o.uid)            as TABLE_SCHEMA,
  19164.         o.name                    as TABLE_NAME,
  19165.         x.rows                    as CARDINALITY
  19166.     from    sysobjects o, sysindexes x
  19167.     where    o.type in ('U')
  19168.     and     o.name = @table_name
  19169.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19170.     and     x.id = o.id
  19171.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19172.     and     permissions(o.id) <> 0
  19173.     order by 2, 3
  19174. go
  19175. dump tran master with no_log
  19176. go
  19177. create procedure sp_statistics_rowset;2
  19178.     (
  19179.     @table_schema    sysname = null     
  19180.     )
  19181. as
  19182.     select    db_name()                as TABLE_CATALOG,        
  19183.         user_name(o.uid)            as TABLE_SCHEMA,
  19184.         o.name                    as TABLE_NAME,
  19185.         x.rows                    as CARDINALITY
  19186.     from    sysobjects o, sysindexes x
  19187.     where    o.type in ('U')
  19188.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19189.     and     x.id = o.id
  19190.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19191.     and     permissions(o.id) <> 0
  19192.     order by 2, 3
  19193. go
  19194.  
  19195. grant execute on sp_statistics_rowset to public
  19196. go
  19197.  
  19198. dump tran master with no_log
  19199. go
  19200. if (charindex('6.00', @@version) > 0)
  19201.     begin
  19202.     if (exists (select * from sysobjects
  19203.             where name = 'sp_statistics_rowset' and type = 'P '))
  19204.         begin
  19205.         drop procedure sp_statistics_rowset
  19206.         dump tran master with no_log
  19207.         end
  19208.     end
  19209. go
  19210.  
  19211.  
  19212. print ''
  19213. print 'creating sp_tables_rowset'
  19214. go
  19215.  
  19216.  
  19217. /*    Procedure for 6.50 and earlier servers */
  19218. create procedure sp_tables_rowset
  19219.     (
  19220.     @table_name    varchar(255), 
  19221.     @table_schema    varchar(255) = null,    
  19222.     @table_type    varchar(255) = null 
  19223.     )
  19224. as
  19225.     select    TABLE_CATALOG    = db_name(),
  19226.         TABLE_SCHEMA    = user_name(o.uid),
  19227.         TABLE_NAME    = o.name,
  19228.         TABLE_TYPE    = convert(varchar(30),
  19229.                     case o.type 
  19230.                     when 'U' then 'TABLE'
  19231.                     when 'V' then 'VIEW'
  19232.                     when 'S' then 'SYSTEM TABLE'
  19233.                     end),
  19234.         TABLE_GUID    = convert(binary(16), null),
  19235.         DESCRIPTION    = convert(varchar(1), null),
  19236.         TABLE_PROPID    = convert(int,null),
  19237.         DATE_CREATED    = o.crdate,
  19238.         DATE_MODIFIED    = convert(datetime,null)
  19239.     from    sysusers u, sysobjects o
  19240.     where    o.type in ('U','V','S')
  19241.     and     o.name = @table_name
  19242.     and     (    @table_schema is null
  19243.         or    @table_schema = user_name(o.uid)
  19244.         )
  19245.     and     (
  19246.             @table_type is null
  19247.         or    @table_type = case o.type 
  19248.                     when 'U' then 'TABLE'
  19249.                     when 'V' then 'VIEW'
  19250.                     when 'S' then 'SYSTEM TABLE'
  19251.                     end
  19252.         )
  19253.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19254.     and     (
  19255.         suser_id() = 1     /* User is the System Administrator */
  19256.         or o.uid = user_id()     /* User created the object */
  19257.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19258.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19259.             from sysprotects p
  19260.             /* join to correlate with all rows in sysobjects */
  19261.             where p.id = o.id
  19262.             /* get rows for public,current user,user's group */
  19263.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19264.             /* check for SELECT,EXECUTE privilege */
  19265.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19266.             ) = 1    /* final magic...compare Grants      */
  19267.         )        
  19268.     order by 4, 2, 3
  19269. go
  19270. dump tran master with no_log
  19271. go
  19272. create procedure sp_tables_rowset;2
  19273.     (
  19274.     @table_schema    varchar(255) = null,    
  19275.     @table_type    varchar(255) = null 
  19276.     )
  19277. as
  19278.     select    TABLE_CATALOG    = db_name(),
  19279.         TABLE_SCHEMA    = user_name(o.uid),
  19280.         TABLE_NAME    = o.name,
  19281.         TABLE_TYPE    = convert(varchar(30),
  19282.                     case o.type 
  19283.                     when 'U' then 'TABLE'
  19284.                     when 'V' then 'VIEW'
  19285.                     when 'S' then 'SYSTEM TABLE'
  19286.                     end),
  19287.         TABLE_GUID    = convert(binary(16), null),
  19288.         DESCRIPTION    = convert(varchar(1), null),
  19289.         TABLE_PROPID    = convert(int,null),
  19290.         DATE_CREATED    = o.crdate,
  19291.         DATE_MODIFIED    = convert(datetime,null)
  19292.     from    sysusers u, sysobjects o
  19293.     where    o.type in ('U','V','S')
  19294.     and     (    @table_schema is null
  19295.         or    @table_schema = user_name(o.uid)
  19296.         )
  19297.     and     (
  19298.             @table_type is null
  19299.         or    @table_type = case o.type 
  19300.                     when 'U' then 'TABLE'
  19301.                     when 'V' then 'VIEW'
  19302.                     when 'S' then 'SYSTEM TABLE'
  19303.                     end
  19304.         )
  19305.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19306.     and     (
  19307.         suser_id() = 1     /* User is the System Administrator */
  19308.         or o.uid = user_id()     /* User created the object */
  19309.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19310.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19311.             from sysprotects p
  19312.             /* join to correlate with all rows in sysobjects */
  19313.             where p.id = o.id
  19314.             /* get rows for public,current user,user's group */
  19315.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19316.             /* check for SELECT,EXECUTE privilege */
  19317.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19318.             ) = 1    /* final magic...compare Grants      */
  19319.         )        
  19320.     order by 4, 2, 3
  19321. go
  19322. dump tran master with no_log
  19323. go
  19324.  
  19325. if (charindex('7.00', @@version) > 0)
  19326.     drop procedure sp_tables_rowset
  19327. else
  19328. begin
  19329.     print ''
  19330.     print ''
  19331.     print 'Warning:'
  19332.     print 'you are installing the stored procedures '
  19333.     print 'on a pre 7.0 SQL Server.'
  19334.     print 'Ignore the following errors.'
  19335. end
  19336. go
  19337.  
  19338. /*    Procedure for 7.00 server */
  19339. create procedure sp_tables_rowset
  19340.     (
  19341.     @table_name    sysname, 
  19342.     @table_schema    sysname = null,    
  19343.     @table_type    nvarchar(255) = null 
  19344.     )
  19345. as
  19346.     select    *
  19347.     from    (select    TABLE_CATALOG    = db_name(),
  19348.             TABLE_SCHEMA    = user_name(uid),
  19349.             TABLE_NAME    = name,
  19350.             TABLE_TYPE    = convert(nvarchar(30),
  19351.                         case type 
  19352.                         when 'U' then 
  19353.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19354.                             then N'TABLE' else N'SYSTEM TABLE' end
  19355.                         when 'S' then N'SYSTEM TABLE'
  19356.                         when 'V' then 
  19357.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19358.                             then N'VIEW' else N'SYSTEM VIEW' end
  19359.                         end),
  19360.             TABLE_GUID    = convert(uniqueidentifier, null),
  19361.             DESCRIPTION    = convert(nvarchar(1), null),
  19362.             TABLE_PROPID    = convert(int,null),
  19363.             DATE_CREATED    = crdate,
  19364.             DATE_MODIFIED    = convert(datetime,null)
  19365.         from    sysobjects
  19366.         where    name = @table_name
  19367.         and    type in ('U','V','S')
  19368.         and    permissions(id) <> 0
  19369.         ) as o
  19370.  
  19371.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19372.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  19373.     order by 4, 2, 3
  19374. go
  19375. dump tran master with no_log
  19376. go
  19377. create procedure sp_tables_rowset;2
  19378.     (
  19379.     @table_schema    sysname = null,    
  19380.     @table_type    nvarchar(255) = null 
  19381.     )
  19382. as
  19383.     select    *
  19384.     from    (select    TABLE_CATALOG    = db_name(),
  19385.             TABLE_SCHEMA    = user_name(uid),
  19386.             TABLE_NAME    = name,
  19387.             TABLE_TYPE    = convert(nvarchar(30),
  19388.                         case type 
  19389.                         when 'U' then 
  19390.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19391.                             then N'TABLE' else N'SYSTEM TABLE' end
  19392.                         when 'S' then N'SYSTEM TABLE'
  19393.                         when 'V' then 
  19394.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  19395.                             then N'VIEW' else N'SYSTEM VIEW' end
  19396.                         end),
  19397.             TABLE_GUID    = convert(uniqueidentifier, null),
  19398.             DESCRIPTION    = convert(nvarchar(1), null),
  19399.             TABLE_PROPID    = convert(int,null),
  19400.             DATE_CREATED    = crdate,
  19401.             DATE_MODIFIED    = convert(datetime,null)
  19402.         from    sysobjects
  19403.         where    type in ('U','V','S')
  19404.         and    permissions(id) <> 0
  19405.         ) as o
  19406.  
  19407.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19408.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  19409.     order by 4, 2, 3
  19410. go
  19411. dump tran master with no_log
  19412. go
  19413. create procedure sp_tables_rowset;5
  19414.     (
  19415.     @table_server        sysname,
  19416.     @table_catalog        sysname = null,
  19417.     @table_name        sysname = null,
  19418.     @table_schema        sysname = null,
  19419.     @table_type        sysname = null
  19420.     )
  19421. as
  19422.     select
  19423.         TABLE_CATALOG,
  19424.         TABLE_SCHEMA,
  19425.         TABLE_NAME,
  19426.         TABLE_TYPE,
  19427.         TABLE_GUID,
  19428.         DESCRIPTION
  19429.     --    TABLE_PROPID,
  19430.     --    DATE_CREATED,
  19431.     --    DATE_MODIFIED
  19432.     from master.dbo.SYSREMOTE_TABLES <
  19433.                 @table_server,
  19434.                 @table_catalog,
  19435.                 @table_schema,
  19436.                 @table_name,
  19437.                 @table_type >
  19438.     order by 4,1,2,3
  19439. go
  19440.  
  19441. grant execute on sp_tables_rowset to public
  19442. go
  19443.  
  19444. dump tran master with no_log
  19445. go
  19446. if (charindex('6.00', @@version) > 0)
  19447.     begin
  19448.     if (exists (select * from sysobjects
  19449.             where name = 'sp_tables_rowset' and type = 'P '))
  19450.         begin
  19451.         drop procedure sp_tables_rowset
  19452.         dump tran master with no_log
  19453.         end
  19454.     end
  19455. go
  19456.  
  19457.  
  19458.  
  19459. print ''
  19460. print 'creating sp_tables_info_rowset'
  19461. go
  19462.  
  19463.  
  19464. /*    Procedure for 6.50 and earlier servers */
  19465. create procedure sp_tables_info_rowset
  19466.     (
  19467.     @table_name    varchar(255), 
  19468.     @table_schema    varchar(255) = null,    
  19469.     @table_type    varchar(255) = null 
  19470.     )
  19471. as
  19472.     select    TABLE_CATALOG        = db_name(),
  19473.         TABLE_SCHEMA        = user_name(o.uid),
  19474.         TABLE_NAME        = o.name,
  19475.         TABLE_TYPE        = convert(varchar(30),
  19476.                         case o.type 
  19477.                         when 'U' then 'TABLE'
  19478.                         when 'V' then 'VIEW'
  19479.                         when 'S' then 'SYSTEM TABLE'
  19480.                         end),
  19481.         TABLE_GUID        = convert(binary(16), null),
  19482.         BOOKMARKS        = convert(bit, 1),
  19483.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19484.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19485.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19486.         BOOKMARK_INFORMATION    = convert(int, 0),
  19487.         TABLE_VERSION        = convert(int, o.schema_ver),
  19488.         CARDINALITY        = x.rows,
  19489.         DESCRIPTION        = convert(varchar(1), null),
  19490.         TABLE_PROPID        = convert(int, null)
  19491.  
  19492.     from    sysusers u, 
  19493.         sysobjects o,
  19494.         sysindexes x
  19495.  
  19496.     where    o.type in ('U','V','S')
  19497.     and     o.name = @table_name
  19498.     and     (    @table_schema is null
  19499.         or    @table_schema = user_name(o.uid)
  19500.         )
  19501.     and     (
  19502.             @table_type is null
  19503.         or    @table_type = case o.type 
  19504.                     when 'U' then 'TABLE'
  19505.                     when 'V' then 'VIEW'
  19506.                     when 'S' then 'SYSTEM TABLE'
  19507.                     end
  19508.         )
  19509.     and    o.id *= x.id
  19510.     and    x.indid in (0,1)
  19511.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19512.     and     (
  19513.         suser_id() = 1     /* User is the System Administrator */
  19514.         or o.uid = user_id()     /* User created the object */
  19515.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19516.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19517.             from sysprotects p
  19518.             /* join to correlate with all rows in sysobjects */
  19519.             where p.id = o.id
  19520.             /* get rows for public,current user,user's group */
  19521.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19522.             /* check for SELECT,EXECUTE privilege */
  19523.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19524.             ) = 1    /* final magic...compare Grants      */
  19525.         )        
  19526.     order by 4, 2, 3
  19527. go
  19528. dump tran master with no_log
  19529. go
  19530. create procedure sp_tables_info_rowset;2
  19531.     (
  19532.     @table_schema    varchar(255) = null,    
  19533.     @table_type    varchar(255) = null 
  19534.     )
  19535. as
  19536.     select    TABLE_CATALOG        = db_name(),
  19537.         TABLE_SCHEMA        = user_name(o.uid),
  19538.         TABLE_NAME        = o.name,
  19539.         TABLE_TYPE        = convert(varchar(30),
  19540.                         case o.type 
  19541.                         when 'U' then 'TABLE'
  19542.                         when 'V' then 'VIEW'
  19543.                         when 'S' then 'SYSTEM TABLE'
  19544.                         end),
  19545.         TABLE_GUID        = convert(binary(16), null),
  19546.         BOOKMARKS        = convert(bit, 1),
  19547.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19548.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19549.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19550.         BOOKMARK_INFORMATION    = convert(int, 0),
  19551.         TABLE_VERSION        = convert(int, o.schema_ver),
  19552.         CARDINALITY        = x.rows,
  19553.         DESCRIPTION        = convert(varchar(1), null),
  19554.         TABLE_PROPID        = convert(int, null)
  19555.  
  19556.     from    sysusers u, 
  19557.         sysobjects o,
  19558.         sysindexes x
  19559.  
  19560.     where    o.type in ('U','V','S')
  19561.     and     (    @table_schema is null
  19562.         or    @table_schema = user_name(o.uid)
  19563.         )
  19564.     and     (
  19565.             @table_type is null
  19566.         or    @table_type = case o.type 
  19567.                     when 'U' then 'TABLE'
  19568.                     when 'V' then 'VIEW'
  19569.                     when 'S' then 'SYSTEM TABLE'
  19570.                     end
  19571.         )
  19572.     and    o.id *= x.id
  19573.     and    x.indid in (0,1)
  19574.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19575.     and     (
  19576.         suser_id() = 1     /* User is the System Administrator */
  19577.         or o.uid = user_id()     /* User created the object */
  19578.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19579.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19580.             from sysprotects p
  19581.             /* join to correlate with all rows in sysobjects */
  19582.             where p.id = o.id
  19583.             /* get rows for public,current user,user's group */
  19584.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19585.             /* check for SELECT,EXECUTE privilege */
  19586.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19587.             ) = 1    /* final magic...compare Grants      */
  19588.         )        
  19589.     order by 4, 2, 3
  19590. go
  19591. dump tran master with no_log
  19592. go
  19593.  
  19594. if (charindex('7.00', @@version) > 0)
  19595.     drop procedure sp_tables_info_rowset
  19596. else
  19597. begin
  19598.     print ''
  19599.     print ''
  19600.     print 'Warning:'
  19601.     print 'you are installing the stored procedures '
  19602.     print 'on a pre 7.0 SQL Server.'
  19603.     print 'Ignore the following errors.'
  19604. end
  19605. go
  19606.  
  19607. /*    Procedure for 7.00 server */
  19608. create procedure sp_tables_info_rowset
  19609.     (
  19610.     @table_name    sysname, 
  19611.     @table_schema    sysname = null,    
  19612.     @table_type    nvarchar(255) = null 
  19613.     )
  19614. as
  19615.     select    *
  19616.     from    (select    TABLE_CATALOG        = db_name(),
  19617.             TABLE_SCHEMA        = user_name(o.uid),
  19618.             TABLE_NAME        = o.name,
  19619.             TABLE_TYPE        = convert(nvarchar(30),
  19620.                             case o.type 
  19621.                             when 'U' then 
  19622.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19623.                                 then N'TABLE' else N'SYSTEM TABLE' end
  19624.                             when 'S' then N'SYSTEM TABLE'
  19625.                             when 'V' then 
  19626.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19627.                                 then N'VIEW' else N'SYSTEM VIEW' end
  19628.                             end),
  19629.             TABLE_GUID        = convert(uniqueidentifier, null),
  19630.             BOOKMARKS        = convert(bit, 1),
  19631.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19632.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19633.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19634.             BOOKMARK_INFORMATION    = convert(int, 0),
  19635.             TABLE_VERSION        = convert(int, o.schema_ver),
  19636.             CARDINALITY        = x.rows,
  19637.             DESCRIPTION        = convert(nvarchar(1), null),
  19638.             TABLE_PROPID        = convert(int, null) 
  19639.     
  19640.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  19641.         where    o.name = @table_name
  19642.         and     o.type in ('U','V','S')
  19643.         and    permissions(o.id) <> 0) as t
  19644.  
  19645.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19646.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  19647.     order by 4, 2, 3
  19648. go
  19649. dump tran master with no_log
  19650. go
  19651. create procedure sp_tables_info_rowset;2
  19652.     (
  19653.     @table_schema    sysname = null,    
  19654.     @table_type    nvarchar(255) = null 
  19655.     )
  19656. as
  19657.     select    *
  19658.     from    (select    TABLE_CATALOG        = db_name(),
  19659.             TABLE_SCHEMA        = user_name(o.uid),
  19660.             TABLE_NAME        = o.name,
  19661.             TABLE_TYPE        = convert(nvarchar(30),
  19662.                             case o.type 
  19663.                             when 'U' then 
  19664.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19665.                                 then N'TABLE' else N'SYSTEM TABLE' end
  19666.                             when 'S' then N'SYSTEM TABLE'
  19667.                             when 'V' then 
  19668.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  19669.                                 then N'VIEW' else N'SYSTEM VIEW' end
  19670.                             end),
  19671.             TABLE_GUID        = convert(uniqueidentifier, null),
  19672.             BOOKMARKS        = convert(bit, 1),
  19673.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  19674.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  19675.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  19676.             BOOKMARK_INFORMATION    = convert(int, 0),
  19677.             TABLE_VERSION        = convert(int, o.schema_ver),
  19678.             CARDINALITY        = x.rows,
  19679.             DESCRIPTION        = convert(nvarchar(1), null),
  19680.             TABLE_PROPID        = convert(int, null) 
  19681.     
  19682.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  19683.         where     o.type in ('U','V','S')
  19684.         and    permissions(o.id) <> 0) as t
  19685.  
  19686.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  19687.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  19688.     order by 4, 2, 3
  19689. go
  19690.  
  19691. grant execute on sp_tables_info_rowset to public
  19692. go
  19693.  
  19694. dump tran master with no_log
  19695. go
  19696. if (charindex('6.00', @@version) > 0)
  19697.     begin
  19698.     if (exists (select * from sysobjects
  19699.         where name = 'sp_tables_info_rowset' and type = 'P '))
  19700.         begin
  19701.         drop procedure sp_tables_info_rowset
  19702.         dump tran master with no_log
  19703.         end
  19704.     end
  19705. go
  19706.  
  19707. print ''
  19708. print 'creating sp_table_constraints_rowset'
  19709. go
  19710.  
  19711.  
  19712. /*    Procedure for 6.50 and earlier servers */
  19713. create procedure sp_table_constraints_rowset
  19714.     (
  19715.     @table_name            varchar(255), 
  19716.     @table_schema        varchar(255) = null,
  19717.     @table_catalog        varchar(255) = null,
  19718.     @constraint_name    varchar(255) = null,
  19719.     @constraint_schema    varchar(255) = null,
  19720.     @constraint_catalog    varchar(255) = null,
  19721.     @constraint_type    varchar(255) = null 
  19722.     )
  19723. as
  19724.     select
  19725.         CONSTRAINT_CATALOG    = db_name(),
  19726.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19727.         CONSTRAINT_NAME        = c_obj.name,
  19728.         TABLE_CATALOG        = db_name(),
  19729.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19730.         TABLE_NAME            = t_obj.name,
  19731.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19732.                                 when 1 then 'PRIMARY KEY'
  19733.                                 when 2 then    'UNIQUE'
  19734.                                 when 3 then    'FOREIGN KEY'
  19735.                                 when 4 then    'CHECK'
  19736.                                  end, 
  19737.         IS_DEFERRABLE        = convert(bit, 0),
  19738.         INITIALLY_DEFERRED    = convert(bit, 0),
  19739.         DESCRIPTION            = convert(varchar(1), null)
  19740.  
  19741.     from 
  19742.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19743.     where
  19744.             t_obj.name    = @table_name
  19745.         and t_obj.type in ('U','S')
  19746.         and (@table_catalog is null or @table_catalog = db_name())
  19747.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19748.         and c.id = t_obj.id
  19749.         and (c.status & 0xf) between 1 and 4
  19750.         and c_obj.id    = c.constid
  19751.         and    c_obj.uid    = user_id()
  19752.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19753.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19754.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19755.         and (@constraint_type is null
  19756.             or (c.status & 0xf)
  19757.                 = case @constraint_type
  19758.                     when 'PRIMARY KEY' then 1
  19759.                     when 'UNIQUE' then 2
  19760.                     when 'FOREIGN KEY' then 3
  19761.                     when 'CHECK' then 4
  19762.                     end)
  19763. order by 2,3,5,6,7 
  19764. go
  19765. dump tran master with no_log
  19766. go
  19767. create procedure sp_table_constraints_rowset;2
  19768.     (
  19769.     @table_schema        varchar(255) = null,
  19770.     @table_catalog        varchar(255) = null,
  19771.     @constraint_name    varchar(255) = null,
  19772.     @constraint_schema    varchar(255) = null,
  19773.     @constraint_catalog    varchar(255) = null,
  19774.     @constraint_type    varchar(255) = null 
  19775.     )
  19776. as
  19777.     select
  19778.         CONSTRAINT_CATALOG    = db_name(),
  19779.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19780.         CONSTRAINT_NAME        = c_obj.name,
  19781.         TABLE_CATALOG        = db_name(),
  19782.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19783.         TABLE_NAME            = t_obj.name,
  19784.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19785.                                 when 1 then 'PRIMARY KEY'
  19786.                                 when 2 then    'UNIQUE'
  19787.                                 when 3 then    'FOREIGN KEY'
  19788.                                 when 4 then    'CHECK'
  19789.                                  end, 
  19790.         IS_DEFERRABLE        = convert(bit, 0),
  19791.         INITIALLY_DEFERRED    = convert(bit, 0),
  19792.         DESCRIPTION            = convert(varchar(1), null)
  19793.  
  19794.     from 
  19795.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19796.     where
  19797.             t_obj.type in ('U','S')
  19798.         and (@table_catalog is null or @table_catalog = db_name())
  19799.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19800.         and c.id = t_obj.id
  19801.         and (c.status & 0xf) between 1 and 4
  19802.         and c_obj.id    = c.constid
  19803.         and    c_obj.uid    = user_id()
  19804.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19805.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19806.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19807.         and (@constraint_type is null
  19808.             or (c.status & 0xf)
  19809.                 = case @constraint_type
  19810.                     when 'PRIMARY KEY' then 1
  19811.                     when 'UNIQUE' then 2
  19812.                     when 'FOREIGN KEY' then 3
  19813.                     when 'CHECK' then 4
  19814.                     end)
  19815. order by 2,3,5,6,7 
  19816. go
  19817. dump tran master with no_log
  19818. go
  19819.  
  19820.  
  19821. if (charindex('7.00', @@version) > 0)
  19822.     drop procedure sp_table_constraints_rowset
  19823. else
  19824. begin
  19825.     print ''
  19826.     print ''
  19827.     print 'Warning:'
  19828.     print 'you are installing the stored procedures '
  19829.     print 'on a pre 7.0 SQL Server.'
  19830.     print 'Ignore the following errors.'
  19831. end
  19832. go
  19833.  
  19834. /*    Procedure for 7.00 server */
  19835. create procedure sp_table_constraints_rowset
  19836.     (
  19837.     @table_name            sysname, 
  19838.     @table_schema        sysname = null,
  19839.     @table_catalog        sysname = null,
  19840.     @constraint_name    sysname = null,
  19841.     @constraint_schema    sysname = null,
  19842.     @constraint_catalog    sysname = null,
  19843.     @constraint_type    nvarchar(255) = null 
  19844.     )
  19845. as
  19846.     select
  19847.         CONSTRAINT_CATALOG    = db_name(),
  19848.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19849.         CONSTRAINT_NAME        = c_obj.name,
  19850.         TABLE_CATALOG        = db_name(),
  19851.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19852.         TABLE_NAME            = t_obj.name,
  19853.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19854.                                 when 1 then N'PRIMARY KEY'
  19855.                                 when 2 then    N'UNIQUE'
  19856.                                 when 3 then    N'FOREIGN KEY'
  19857.                                 when 4 then    N'CHECK'
  19858.                                  end, 
  19859.         IS_DEFERRABLE        = convert(bit, 0),
  19860.         INITIALLY_DEFERRED    = convert(bit, 0),
  19861.         DESCRIPTION            = convert(nvarchar(1), null)
  19862.  
  19863.     from 
  19864.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19865.     where
  19866.             t_obj.name    = @table_name
  19867.         and t_obj.type in ('U','S')
  19868.         and (@table_catalog is null or @table_catalog = db_name())
  19869.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19870.         and c.id = t_obj.id
  19871.         and (c.status & 0xf) between 1 and 4
  19872.         and c_obj.id    = c.constid
  19873.         and    c_obj.uid    = user_id()
  19874.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19875.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19876.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19877.         and (@constraint_type is null
  19878.             or (c.status & 0xf)
  19879.                 = case @constraint_type
  19880.                     when N'PRIMARY KEY' then 1
  19881.                     when N'UNIQUE' then 2
  19882.                     when N'FOREIGN KEY' then 3
  19883.                     when N'CHECK' then 4
  19884.                     end)
  19885. order by 2,3,5,6,7 
  19886. go
  19887. dump tran master with no_log
  19888. go
  19889. create procedure sp_table_constraints_rowset;2
  19890.     (
  19891.     @table_schema        sysname = null,
  19892.     @table_catalog        sysname = null,
  19893.     @constraint_name    sysname = null,
  19894.     @constraint_schema    sysname = null,
  19895.     @constraint_catalog    sysname = null,
  19896.     @constraint_type    nvarchar(255) = null 
  19897.     )
  19898. as
  19899.     select
  19900.         CONSTRAINT_CATALOG    = db_name(),
  19901.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  19902.         CONSTRAINT_NAME        = c_obj.name,
  19903.         TABLE_CATALOG        = db_name(),
  19904.         TABLE_SCHEMA        = user_name(t_obj.uid),
  19905.         TABLE_NAME            = t_obj.name,
  19906.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  19907.                                 when 1 then N'PRIMARY KEY'
  19908.                                 when 2 then    N'UNIQUE'
  19909.                                 when 3 then    N'FOREIGN KEY'
  19910.                                 when 4 then    N'CHECK'
  19911.                                  end, 
  19912.         IS_DEFERRABLE        = convert(bit, 0),
  19913.         INITIALLY_DEFERRED    = convert(bit, 0),
  19914.         DESCRIPTION            = convert(nvarchar(1), null)
  19915.  
  19916.     from 
  19917.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  19918.     where
  19919.             t_obj.type in ('U','S')
  19920.         and (@table_catalog is null or @table_catalog = db_name())
  19921.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  19922.         and c.id = t_obj.id
  19923.         and (c.status & 0xf) between 1 and 4
  19924.         and c_obj.id    = c.constid
  19925.         and    c_obj.uid    = user_id()
  19926.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  19927.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  19928.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  19929.         and (@constraint_type is null
  19930.             or (c.status & 0xf)
  19931.                 = case @constraint_type
  19932.                     when N'PRIMARY KEY' then 1
  19933.                     when N'UNIQUE' then 2
  19934.                     when N'FOREIGN KEY' then 3
  19935.                     when N'CHECK' then 4
  19936.                     end)
  19937. order by 2,3,5,6,7 
  19938. go
  19939. dump tran master with no_log
  19940.  
  19941.  
  19942. grant execute on sp_table_constraints_rowset to public
  19943. go
  19944.  
  19945. dump tran master with no_log
  19946. go
  19947. if (charindex('6.00', @@version) > 0)
  19948.     begin
  19949.     if (exists (select * from sysobjects
  19950.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  19951.         begin
  19952.         drop procedure sp_table_constraints_rowset
  19953.         dump tran master with no_log
  19954.         end
  19955.     end
  19956. go
  19957.  
  19958. print ''
  19959. print 'creating sp_table_privileges_rowset'
  19960. go
  19961.  
  19962. /*    Procedure for 6.0 and 6.5 server */
  19963. CREATE PROCEDURE sp_table_privileges_rowset
  19964.     (
  19965.     @table_name    varchar(255) = null,
  19966.     @table_schema    varchar(255) = null,
  19967.     @grantor    varchar(255) = null,
  19968.     @grantee    varchar(255) = null
  19969.     )
  19970. as
  19971. IF @table_name is not null
  19972.     BEGIN
  19973.     select
  19974.         GRANTOR        = user_name(p.grantor),
  19975.         GRANTEE        = user_name(u.uid),
  19976.         TABLE_CATALOG    = db_name(),
  19977.         TABLE_SCHEMA    = user_name(o.uid),
  19978.         TABLE_NAME    = o.name,
  19979.         PRIVILEGE_TYPE    = convert(varchar(30),
  19980.                     case p.action
  19981.                     when 193 then 'SELECT'
  19982.                     when 195 then 'INSERT'
  19983.                     when 196 then 'DELETE'
  19984.                     when 197 then 'UPDATE'
  19985.                     else 'REFERENCES'
  19986.                     end),
  19987.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19988.     from 
  19989.         sysprotects p, sysobjects o, sysusers u
  19990.     where
  19991.         o.name = @table_name
  19992.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19993.     and    (@grantee is null or @grantee = user_name(u.uid))
  19994.     and     o.type in ('U','V','S')
  19995.     and    p.id = o.id
  19996.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19997.             /* expand groups */
  19998.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19999.     and     p.protecttype <> 206    /* only grant rows */
  20000.     and     p.action in (26,193,195,196,197)
  20001.     and     o.uid <> u.uid            /* no rows for owner */
  20002.     and     not exists (            /* exclude revoke'd privileges */
  20003.             select     *
  20004.             from     sysprotects p1
  20005.             where    p1.protecttype = 206
  20006.             and     p1.action = p.action
  20007.             and     p1.id = p.id
  20008.             and     p1.uid = u.uid)
  20009.     union all
  20010.     select    /*    Add rows for table owner */
  20011.         GRANTOR        = user_name(u.uid),
  20012.         GRANTEE        = user_name(o.uid),
  20013.         TABLE_CATALOG    = db_name(),
  20014.         TABLE_SCHEMA    = user_name(o.uid),
  20015.         TABLE_NAME    = o.name,
  20016.         PRIVILEGE_TYPE    = convert(varchar(30),
  20017.                     case v.number
  20018.                     when 193 then 'SELECT'
  20019.                     when 195 then 'INSERT'
  20020.                     when 196 then 'DELETE'
  20021.                     when 197 then 'UPDATE'
  20022.                     else 'REFERENCES'
  20023.                     end),
  20024.         IS_GRANTABLE    = convert(bit,1)    
  20025.     from 
  20026.         sysobjects o, master.dbo.spt_values v, sysusers u
  20027.     where
  20028.         o.name = @table_name
  20029.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20030.     and    (@grantee is null or @grantee = user_name(u.uid))
  20031.     and     o.type in ('U','V','S')
  20032.     and     u.suid = 1        /* grantor is dbo of database */
  20033.     and    (@grantor is null or @grantor = user_name(u.uid))
  20034.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20035.     and     v.number in (26,193,195,196,197)
  20036.     and     not exists (    /* exclude revoke'd privileges */
  20037.             select     *
  20038.             from     sysprotects p1
  20039.             where    p1.protecttype = 206
  20040.             and     p1.action = v.number
  20041.             and     p1.id = o.id
  20042.             and     p1.uid = o.uid)
  20043.     order by 4,5,6,1,2
  20044.     END
  20045. ELSE
  20046.     BEGIN
  20047.     select
  20048.         GRANTOR        = user_name(p.grantor),
  20049.         GRANTEE        = user_name(u.uid),
  20050.         TABLE_CATALOG    = db_name(),
  20051.         TABLE_SCHEMA    = user_name(o.uid),
  20052.         TABLE_NAME    = o.name,
  20053.         PRIVILEGE_TYPE    = convert(varchar(30),
  20054.                     case p.action
  20055.                     when 193 then 'SELECT'
  20056.                     when 195 then 'INSERT'
  20057.                     when 196 then 'DELETE'
  20058.                     when 197 then 'UPDATE'
  20059.                     else 'REFERENCES'
  20060.                     end),
  20061.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20062.     from 
  20063.         sysprotects p, sysobjects o, sysusers u
  20064.     where
  20065.         o.type in ('U','V','S')
  20066.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20067.     and    (@grantee is null or @grantee = user_name(u.uid))
  20068.     and    p.id = o.id
  20069.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20070.             /* expand groups */
  20071.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20072.     and     p.protecttype <> 206    /* only grant rows */
  20073.     and     p.action in (26,193,195,196,197)
  20074.     and     o.uid <> u.uid            /* no rows for owner */
  20075.     and     not exists (            /* exclude revoke'd privileges */
  20076.             select     *
  20077.             from     sysprotects p1
  20078.             where    p1.protecttype = 206
  20079.             and     p1.action = p.action
  20080.             and     p1.id = p.id
  20081.             and     p1.uid = u.uid)
  20082.     union all
  20083.     select    /*    Add rows for table owner */
  20084.         GRANTOR        = user_name(u.uid),
  20085.         GRANTEE        = user_name(o.uid),
  20086.         TABLE_CATALOG    = db_name(),
  20087.         TABLE_SCHEMA    = user_name(o.uid),
  20088.         TABLE_NAME    = o.name,
  20089.         PRIVILEGE_TYPE    = convert(varchar(30),
  20090.                     case v.number
  20091.                     when 193 then 'SELECT'
  20092.                     when 195 then 'INSERT'
  20093.                     when 196 then 'DELETE'
  20094.                     when 197 then 'UPDATE'
  20095.                     else 'REFERENCES'
  20096.                     end),
  20097.         IS_GRANTABLE    = convert(bit,1)    
  20098.     from 
  20099.         sysobjects o, master.dbo.spt_values v, sysusers u
  20100.     where
  20101.         o.type in ('U','V','S')
  20102.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20103.     and    (@grantee is null or @grantee = user_name(u.uid))
  20104.     and     u.suid = 1        /* grantor is dbo of database */
  20105.     and    (@grantor is null or @grantor = user_name(u.uid))
  20106.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20107.     and     v.number in (26,193,195,196,197)
  20108.     and     not exists (    /* exclude revoke'd privileges */
  20109.             select     *
  20110.             from     sysprotects p1
  20111.             where    p1.protecttype = 206
  20112.             and     p1.action = v.number
  20113.             and     p1.id = o.id
  20114.             and     p1.uid = o.uid)
  20115.     order by 4,5,6,1,2
  20116.     END
  20117. go
  20118. dump tran master with no_log
  20119. go
  20120. CREATE PROCEDURE sp_table_privileges_rowset;2
  20121.     (
  20122.        @handle        int output,
  20123.        @scrollopt    int output,
  20124.     @ccopt        int output,
  20125.     @rows        int output,
  20126.     @table_name     varchar(255) = null,
  20127.     @table_schema    varchar(255) = null,
  20128.     @grantor    varchar(255) = null,
  20129.     @grantee    varchar(255) = null
  20130.     )
  20131. as
  20132. declare @ret int
  20133. SET NOCOUNT ON
  20134. create table #sptprivsrowset1
  20135.     (
  20136.     GRANTOR        sysname not null,
  20137.     GRANTEE        sysname not null,
  20138.     TABLE_CATALOG    sysname not null,
  20139.     TABLE_SCHEMA    sysname not null,
  20140.     TABLE_NAME    sysname not null,
  20141.     PRIVILEGE_TYPE    sysname not null,
  20142.     IS_GRANTABLE    bit not null
  20143.     )
  20144.     
  20145. IF @table_name is not null
  20146.     BEGIN
  20147.     insert into #sptprivsrowset1
  20148.     select
  20149.         GRANTOR        = user_name(p.grantor),
  20150.         GRANTEE        = user_name(u.uid),
  20151.         TABLE_CATALOG    = db_name(),
  20152.         TABLE_SCHEMA    = user_name(o.uid),
  20153.         TABLE_NAME    = o.name,
  20154.         PRIVILEGE_TYPE    = convert(varchar(30),
  20155.                     case p.action
  20156.                     when 193 then 'SELECT'
  20157.                     when 195 then 'INSERT'
  20158.                     when 196 then 'DELETE'
  20159.                     when 197 then 'UPDATE'
  20160.                     else 'REFERENCES'
  20161.                     end),
  20162.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20163.     from 
  20164.         sysprotects p, sysobjects o, sysusers u
  20165.     where
  20166.         o.name = @table_name
  20167.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20168.     and    (@grantee is null or @grantee = user_name(u.uid))
  20169.     and     o.type in ('U','V','S')
  20170.     and    p.id = o.id
  20171.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20172.             /* expand groups */
  20173.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20174.     and     p.protecttype <> 206    /* only grant rows */
  20175.     and     p.action in (26,193,195,196,197)
  20176.     and     o.uid <> u.uid            /* no rows for owner */
  20177.     and     not exists (            /* exclude revoke'd privileges */
  20178.             select     *
  20179.             from     sysprotects p1
  20180.             where    p1.protecttype = 206
  20181.             and     p1.action = p.action
  20182.             and     p1.id = p.id
  20183.             and     p1.uid = u.uid)
  20184.     union all
  20185.     select    /*    Add rows for table owner */
  20186.         GRANTOR        = user_name(u.uid),
  20187.         GRANTEE        = user_name(o.uid),
  20188.         TABLE_CATALOG    = db_name(),
  20189.         TABLE_SCHEMA    = user_name(o.uid),
  20190.         TABLE_NAME    = o.name,
  20191.         PRIVILEGE_TYPE    = convert(varchar(30),
  20192.                     case v.number
  20193.                     when 193 then 'SELECT'
  20194.                     when 195 then 'INSERT'
  20195.                     when 196 then 'DELETE'
  20196.                     when 197 then 'UPDATE'
  20197.                     else 'REFERENCES'
  20198.                     end),
  20199.         IS_GRANTABLE    = convert(bit,1)    
  20200.     from 
  20201.         sysobjects o, master.dbo.spt_values v, sysusers u
  20202.     where
  20203.         o.name = @table_name
  20204.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20205.     and    (@grantee is null or @grantee = user_name(u.uid))
  20206.     and     o.type in ('U','V','S')
  20207.     and     u.suid = 1        /* grantor is dbo of database */
  20208.     and    (@grantor is null or @grantor = user_name(u.uid))
  20209.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20210.     and     v.number in (26,193,195,196,197)
  20211.     and     not exists (    /* exclude revoke'd privileges */
  20212.             select     *
  20213.             from     sysprotects p1
  20214.             where    p1.protecttype = 206
  20215.             and     p1.action = v.number
  20216.             and     p1.id = o.id
  20217.             and     p1.uid = o.uid)
  20218.     order by 4,5,6,1,2
  20219.     END
  20220. ELSE
  20221.     BEGIN
  20222.     insert into #sptprivsrowset1
  20223.     select
  20224.         GRANTOR        = user_name(p.grantor),
  20225.         GRANTEE        = user_name(u.uid),
  20226.         TABLE_CATALOG    = db_name(),
  20227.         TABLE_SCHEMA    = user_name(o.uid),
  20228.         TABLE_NAME    = o.name,
  20229.         PRIVILEGE_TYPE    = convert(varchar(30),
  20230.                     case p.action
  20231.                     when 193 then 'SELECT'
  20232.                     when 195 then 'INSERT'
  20233.                     when 196 then 'DELETE'
  20234.                     when 197 then 'UPDATE'
  20235.                     else 'REFERENCES'
  20236.                     end),
  20237.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20238.     from 
  20239.         sysprotects p, sysobjects o, sysusers u
  20240.     where
  20241.         o.type in ('U','V','S')
  20242.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20243.     and    (@grantee is null or @grantee = user_name(u.uid))
  20244.     and    p.id = o.id
  20245.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20246.             /* expand groups */
  20247.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  20248.     and     p.protecttype <> 206    /* only grant rows */
  20249.     and     p.action in (26,193,195,196,197)
  20250.     and     o.uid <> u.uid            /* no rows for owner */
  20251.     and     not exists (            /* exclude revoke'd privileges */
  20252.             select     *
  20253.             from     sysprotects p1
  20254.             where    p1.protecttype = 206
  20255.             and     p1.action = p.action
  20256.             and     p1.id = p.id
  20257.             and     p1.uid = u.uid)
  20258.     union all
  20259.     select    /*    Add rows for table owner */
  20260.         GRANTOR        = user_name(u.uid),
  20261.         GRANTEE        = user_name(o.uid),
  20262.         TABLE_CATALOG    = db_name(),
  20263.         TABLE_SCHEMA    = user_name(o.uid),
  20264.         TABLE_NAME    = o.name,
  20265.         PRIVILEGE_TYPE    = convert(varchar(30),
  20266.                     case v.number
  20267.                     when 193 then 'SELECT'
  20268.                     when 195 then 'INSERT'
  20269.                     when 196 then 'DELETE'
  20270.                     when 197 then 'UPDATE'
  20271.                     else 'REFERENCES'
  20272.                     end),
  20273.         IS_GRANTABLE    = convert(bit,1)    
  20274.     from 
  20275.         sysobjects o, master.dbo.spt_values v, sysusers u
  20276.     where
  20277.         o.type in ('U','V','S')
  20278.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20279.     and    (@grantee is null or @grantee = user_name(u.uid))
  20280.     and     u.suid = 1        /* grantor is dbo of database */
  20281.     and    (@grantor is null or @grantor = user_name(u.uid))
  20282.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20283.     and     v.number in (26,193,195,196,197)
  20284.     and     not exists (    /* exclude revoke'd privileges */
  20285.             select     *
  20286.             from     sysprotects p1
  20287.             where    p1.protecttype = 206
  20288.             and     p1.action = v.number
  20289.             and     p1.id = o.id
  20290.             and     p1.uid = o.uid)
  20291.     order by 4,5,6,1,2
  20292.     END
  20293.  
  20294. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  20295.     @scrollopt output, @ccopt output, @rows output
  20296.  
  20297. drop table #sptprivsrowset1
  20298. return isnull(@ret,0)
  20299. go
  20300. dump tran master with no_log
  20301. go
  20302. CREATE PROCEDURE sp_table_privileges_rowset;3
  20303. as
  20304.     select
  20305.         GRANTOR        = convert(sysname, ' '),
  20306.         GRANTEE        = convert(sysname, ' '),
  20307.         TABLE_CATALOG    = convert(sysname, ' '),
  20308.         TABLE_SCHEMA    = convert(sysname, ' '),
  20309.         TABLE_NAME    = convert(sysname, ' '),
  20310.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  20311.         IS_GRANTABLE    = convert(bit, 0)
  20312.     where    1=0
  20313. go
  20314.  
  20315. if (charindex('7.00', @@version) = 0)
  20316. begin
  20317.     print ''
  20318.     print ''
  20319.     print 'Warning:'
  20320.     print 'you are installing the stored procedures '
  20321.     print 'on a pre 7.0 SQL Server.'
  20322.     print 'Ignore the following errors.'
  20323. end
  20324. else
  20325.     drop proc sp_table_privileges_rowset
  20326. go
  20327.  
  20328.  
  20329. /*    Procedure for 7.0 server */
  20330. CREATE PROCEDURE sp_table_privileges_rowset
  20331.     (
  20332.     @table_name         sysname,
  20333.     @table_schema        sysname = null,
  20334.     @grantor        sysname = null,
  20335.     @grantee        sysname = null
  20336.     )
  20337. as
  20338.     select
  20339.         GRANTOR        = user_name(p.grantor),
  20340.         GRANTEE        = user_name(u.uid),
  20341.         TABLE_CATALOG    = db_name(),
  20342.         TABLE_SCHEMA    = user_name(o.uid),
  20343.         TABLE_NAME    = o.name,
  20344.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20345.                     case p.action
  20346.                     when 193 then N'SELECT'
  20347.                     when 195 then N'INSERT'
  20348.                     when 196 then N'DELETE'
  20349.                     when 197 then N'UPDATE'
  20350.                     else N'REFERENCES'
  20351.                     end),
  20352.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20353.     from 
  20354.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20355.     where
  20356.         o.name = @table_name
  20357.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20358.     and    (@grantee is null or @grantee = user_name(u.uid))
  20359.     and     o.type in ('U','V','S')
  20360.     and    p.id = o.id
  20361.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20362.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20363.     and     (u.uid > 0 and u.uid < 16384)
  20364.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20365.     and     p.protecttype <> 206    /* only grant rows */
  20366.     and     p.action in (26,193,195,196,197)
  20367.     and     o.uid <> u.uid            /* no rows for owner */
  20368.     and     not exists (            /* exclude revoke'd privileges */
  20369.             select     *
  20370.             from     sysprotects p1
  20371.             where    p1.protecttype = 206
  20372.             and     p1.action = p.action
  20373.             and     p1.id = p.id
  20374.             and     p1.uid = u.uid)
  20375.     union all
  20376.     select    /*    Add rows for table owner */
  20377.         GRANTOR        = user_name(u.uid),
  20378.         GRANTEE        = user_name(o.uid),
  20379.         TABLE_CATALOG    = db_name(),
  20380.         TABLE_SCHEMA    = user_name(o.uid),
  20381.         TABLE_NAME    = o.name,
  20382.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20383.                     case v.number
  20384.                     when 193 then N'SELECT'
  20385.                     when 195 then N'INSERT'
  20386.                     when 196 then N'DELETE'
  20387.                     when 197 then N'UPDATE'
  20388.                     else N'REFERENCES'
  20389.                     end),
  20390.         IS_GRANTABLE    = convert(bit,1)    
  20391.     from 
  20392.         sysobjects o, master.dbo.spt_values v, sysusers u
  20393.     where
  20394.         o.name = @table_name
  20395.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20396.     and    (@grantee is null or @grantee = user_name(u.uid))
  20397.     and     o.type in ('U','V','S')
  20398.     and     u.suid = 1        /* grantor is dbo of database */
  20399.     and    (@grantor is null or @grantor = user_name(u.uid))
  20400.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20401.     and     v.number in (26,193,195,196,197)
  20402.     and     not exists (    /* exclude revoke'd privileges */
  20403.             select     *
  20404.             from     sysprotects p1
  20405.             where    p1.protecttype = 206
  20406.             and     p1.action = v.number
  20407.             and     p1.id = o.id
  20408.             and     p1.uid = o.uid)
  20409.     order by 4,5,6,1,2
  20410. go
  20411. dump tran master with no_log
  20412. go
  20413. CREATE PROCEDURE sp_table_privileges_rowset;2
  20414.     (
  20415.     @table_schema        sysname = null,
  20416.     @grantor        sysname = null,
  20417.     @grantee        sysname = null
  20418.     )
  20419. as
  20420.     select
  20421.         GRANTOR        = user_name(p.grantor),
  20422.         GRANTEE        = user_name(u.uid),
  20423.         TABLE_CATALOG    = db_name(),
  20424.         TABLE_SCHEMA    = user_name(o.uid),
  20425.         TABLE_NAME    = o.name,
  20426.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20427.                     case p.action
  20428.                     when 193 then N'SELECT'
  20429.                     when 195 then N'INSERT'
  20430.                     when 196 then N'DELETE'
  20431.                     when 197 then N'UPDATE'
  20432.                     else N'REFERENCES'
  20433.                     end),
  20434.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20435.     from 
  20436.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20437.     where
  20438.         o.type in ('U','V','S')
  20439.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20440.     and    (@grantee is null or @grantee = user_name(u.uid))
  20441.     and    p.id = o.id
  20442.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20443.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20444.     and     (u.uid > 0 and u.uid < 16384)
  20445.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20446.     and     p.protecttype <> 206    /* only grant rows */
  20447.     and     p.action in (26,193,195,196,197)
  20448.     and     o.uid <> u.uid            /* no rows for owner */
  20449.     and     not exists (            /* exclude revoke'd privileges */
  20450.             select     *
  20451.             from     sysprotects p1
  20452.             where    p1.protecttype = 206
  20453.             and     p1.action = p.action
  20454.             and     p1.id = p.id
  20455.             and     p1.uid = u.uid)
  20456.     union all
  20457.     select    /*    Add rows for table owner */
  20458.         GRANTOR        = user_name(u.uid),
  20459.         GRANTEE        = user_name(o.uid),
  20460.         TABLE_CATALOG    = db_name(),
  20461.         TABLE_SCHEMA    = user_name(o.uid),
  20462.         TABLE_NAME    = o.name,
  20463.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  20464.                     case v.number
  20465.                     when 193 then N'SELECT'
  20466.                     when 195 then N'INSERT'
  20467.                     when 196 then N'DELETE'
  20468.                     when 197 then N'UPDATE'
  20469.                     else N'REFERENCES'
  20470.                     end),
  20471.         IS_GRANTABLE    = convert(bit,1)    
  20472.     from 
  20473.         sysobjects o, master.dbo.spt_values v, sysusers u
  20474.     where
  20475.         o.type in ('U','V','S')
  20476.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20477.     and    (@grantee is null or @grantee = user_name(u.uid))
  20478.     and     u.suid = 1        /* grantor is dbo of database */
  20479.     and    (@grantor is null or @grantor = user_name(u.uid))
  20480.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20481.     and     v.number in (26,193,195,196,197)
  20482.     and     not exists (    /* exclude revoke'd privileges */
  20483.             select     *
  20484.             from     sysprotects p1
  20485.             where    p1.protecttype = 206
  20486.             and     p1.action = v.number
  20487.             and     p1.id = o.id
  20488.             and     p1.uid = o.uid)
  20489.     order by 4,5,6,1,2
  20490. go
  20491. dump tran master with no_log
  20492. go
  20493. create procedure sp_table_privileges_rowset;5
  20494.     (
  20495.     @table_server        sysname,
  20496.     @table_catalog        sysname = null,
  20497.     @table_name        sysname = null,
  20498.     @table_schema        sysname = null,
  20499.     @grantor        sysname = null,
  20500.     @grantee        sysname = null
  20501.     )
  20502. as
  20503.     select
  20504.         GRANTOR,        
  20505.         GRANTEE,        
  20506.         TABLE_CATALOG,    
  20507.         TABLE_SCHEMA,    
  20508.         TABLE_NAME,    
  20509.         PRIVILEGE_TYPE,    
  20510.         IS_GRANTABLE    
  20511.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  20512.                 @table_server,
  20513.                 @table_catalog,
  20514.                 @table_schema,
  20515.                 @table_name,
  20516.                 @grantor,
  20517.                 @grantee >
  20518.     order by 3,4,5,6,1,2
  20519. go
  20520.  
  20521. grant execute on sp_table_privileges_rowset to public
  20522. go
  20523. dump tran master with no_log
  20524. go
  20525. if (charindex('6.00', @@version) > 0)
  20526.     begin
  20527.     if (exists (select * from sysobjects
  20528.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  20529.         begin
  20530.         drop proc sp_table_privileges_rowset
  20531.         dump tran master with no_log
  20532.         end
  20533.     end
  20534. go
  20535.  
  20536.  
  20537. print ''
  20538. print 'creating sp_linkedservers_rowset'
  20539. go
  20540. if (charindex('7.00', @@version) = 0)
  20541. begin
  20542.     print ''
  20543.     print ''
  20544.     print 'Warning:'
  20545.     print 'you are installing the stored procedures '
  20546.     print 'on a pre 7.0 SQL Server.'
  20547.     print 'Ignore the following errors.'
  20548. end
  20549. go
  20550. /*    Procedure for 7.0 server */
  20551. create proc sp_linkedservers_rowset
  20552.     (
  20553.     @srvname     sysname
  20554.     )
  20555. as
  20556.     select
  20557.         SVR_NAME         = srvname,
  20558.         SVR_PRODUCT        = srvproduct,
  20559.         SVR_PROVIDERNAME    = providername,
  20560.         SVR_DATASOURCE        = datasource,
  20561.         SVR_PROVIDERSTRING    = providerstring,
  20562.         SVR_LOCATION        = location,
  20563.         SVR_CATALOG        = catalog
  20564.     from master.dbo.sysservers
  20565.     where srvname = @srvname and (srvstatus & 128) = 128
  20566.     order by 1
  20567. go
  20568. dump tran master with no_log
  20569. go
  20570. create proc sp_linkedservers_rowset;2
  20571. as
  20572.     select
  20573.         SVR_NAME         = srvname,
  20574.         SVR_PRODUCT        = srvproduct,
  20575.         SVR_PROVIDERNAME    = providername,
  20576.         SVR_DATASOURCE        = datasource,
  20577.         SVR_PROVIDERSTRING    = providerstring,
  20578.         SVR_LOCATION        = location,
  20579.         SVR_CATALOG        = catalog
  20580.     from master.dbo.sysservers
  20581.     where (srvstatus & 128) = 128
  20582.     order by 1
  20583. go
  20584.  
  20585. grant execute on sp_linkedservers_rowset to public
  20586. go
  20587.  
  20588. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  20589.  
  20590.  
  20591. dump tran master with no_log
  20592. go
  20593.  
  20594.  
  20595.  
  20596.  
  20597. if (exists (select * from sysobjects
  20598.         where name = 'sp_configure' and type = 'P '))
  20599.     begin
  20600.         exec sp_configure 'allow updates',0
  20601.         reconfigure with override
  20602.     end
  20603. go
  20604.  
  20605. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  20606.  
  20607. go
  20608.  
  20609. if exists (select * from sysobjects where name = 'sp_check_objects'
  20610.             and type = 'P ')
  20611.     begin
  20612.         /* Only supported on 6.0 servers */
  20613.         print ''
  20614.         print 'Checking objects created by instcat.sql.'
  20615.  
  20616.         exec sp_check_objects 'catalog'
  20617.     end
  20618. go
  20619.  
  20620. print ''
  20621. print 'instcat.sql completed successfully.'
  20622. go
  20623.  
  20624. set quoted_identifier off
  20625. go
  20626.  
  20627. dump tran master with no_log
  20628. go
  20629. checkpoint
  20630. go
  20631. /**/
  20632.